How To Send Ajax Call Only Once In My Case
I am confused about how to write this logic. What I am looking for is when the video is playing and if the video watched percentage is more than 2% I want to send an AJAX request
Solution 1:
You can use a var triggered = 0;
before the "if".
And then the "if" becomes: if(totalPlayed > 0.02*(totalTime) && !triggered ){triggered = 1;
This will be executed just once because when you enter the "if" you will update the "triggered" variable and it won't enter the "if" again.
Solution 2:
Use a variable. When you enter ajax, you change the variable. Use the variable within your IF.
JS:
var var_tr = $(#hideninput).val();
if(totalPlayed > 0.02*(totalTime) && Var_tr == "false") {
...
$(#hideninput).val("True");
/* 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!");
$(#hideninput).val("false");
}
});
/* Ajax call video watched ends here */
}
HTML create:
<inputid = "hideninput"type = "hidden" value = "false">
Post a Comment for "How To Send Ajax Call Only Once In My Case"