mercredi 3 mai 2017

how to send ajax call only once in my case

I am confused to how to write if logic.. what I am actually looking is when the video is playing if the video watched percentage is more than 2% I want to send ajax call once. But actually what happening is it will be keep on triggering from 2% to 100%

if I write =(equals) instead of >(greater than) it is not triggering because I am not rounding the number so due to decimals it is not triggering. how can I write the logic in this case. Ajax call should only sent one time.

var j=0;
$("#video").bind("timeupdate", function() {
var currentTime = this.currentTime;
var totalPlayed = 0;
var played = video.played;
for (var i = 0; i < played.length; i++) {
    totalPlayed += played.end(i) - played.start(i);
    var  playedInPercentage = (totalPlayed/totalTime)*100;
}
/*here i want to write a logic to send this lowered only once.*/
if(totalPlayed > 0.02*(totalTime)) { //watched 2% and above then this will excute 0.02(2%)
    alert("asdsad");
    if(j<1) {
        /* Ajax call  video watched starts here it should trigger only once*/
        $.ajax({
            url: "student_learning_controller.php",
            method: "POST",
            data: {'action': 'videoWatched', 'course_id': course_id,'videoId' : videoId,'totalVideoTime':totalTime,'totalPlayedTime':totalPlayed,'percentagePlayed':playedInPercentage },
            dataType: "json",

            success: function (response) {
                if (response["success"] == true) {
                    $("#success_message").html(response["message"]);    

                }
            },
            error: function (request, status, error) {
                $("#warning_message").show();
                $("#warning_message").html("OOPS! Something Went Wrong Please Try After Sometime!");
            }
        });
        /* Ajax call  video watched ends here */
    }
    j=j+1; //Reset for duplicates (repeated playback)
}
});     

Aucun commentaire:

Enregistrer un commentaire