How can get returned values from jquery $.get?

17 Views Asked by At

Bellow is my code. It works now, but if I delete the following line alert(currentIntervalPosition+"__1"); from the getIncidentTime function, the code will not work, and I need to delete that line. When I say the code doesn't work, means that in the initializeVideoInformation function, after getIncidentTime call, variable currentIntervalPosition is undefined. How can I access the returned data?

   function initializeVideoInformation(json) {
        $.each(json, function(idx, obj) {
            if (obj) {
                var result = obj.timeStamp.split(" ");
                var dateFolder = result[0].replace(/-/g, "_");
                var videoName = result[1].substr(0, 2);
                var videoFileWithPath =  "/video/"+dateFolder+"/"+videoName;
                var syncFile = videoFileWithPath + ".sync";
                videoFileWithPath += ".mp4";

                var currentIntervalPosition = getIncidentTime(syncFile, result[1]);
                getVideoScreenShot(videoFileWithPath, currentIntervalPosition, idx);
            }
        });
    }

    function getIncidentTime(syncFile, incidentTime) {
        var currentIntervalPosition;
        $.get(syncFile, function(json) {
            var fileContent = JSON.parse(json);
            var fps = fileContent.fps;
            var frames = fileContent.frames;
            var syncData = [];

            for (var index=0; index<frames.length; index++) {
                syncData[index] = timeToMillies(frames[index].t);
            }

            var jumpTime = timeToMillies(incidentTime);
            var BinaryIndex = BinarySearch(jumpTime, syncData);
            var currentTime = (1 + BinaryIndex) / 25 + 0.0001;    // fps

            var frameNo = Math.floor(25*currentTime) - 1;   //fps
            currentIntervalPosition = syncData[frameNo];
        });

        alert(currentIntervalPosition+"__1");
        return currentIntervalPosition;
    }
0

There are 0 best solutions below