php - How can I be sure of jquery ajax call is executing for each visitor?

I have a php counter "counter.php" called from a javascript file. I want to be sure, for each and every visitor this counter.php is called when the dom finsihes loading. I wrote this piece of code :
$(document).ready(function (){
$.ajax({
url:"https://www.cloudflare.com/cdn-cgi/trace",
cache:false,
timeout:5000,
success:function(data){
cc=data.match(/loc=(.*)\n/);
$.get('counter.php?cc='+cc[1]);
},
error:function(){
$.get('counter.php');
}
});
});
I have jquery embedded. Is this enough to be sure the counter.php file is called for each and every visit? I mean is the ajax call executed for each visit? Is there any possibility that either success: or error: functions of ajax call cannot be executed?
Answer
Solution:
You can update another counter in a complete callback to verify each user has triggered the call. Match the complete counter with unique visit counters and you can be sure this is being fired, whether error or success.
https://api.jquery.com/ajaxcomplete/
Whenever an Ajax request completes, jQuery triggers the ajaxComplete event. Any and all handlers that have been registered with the .ajaxComplete() method are executed at this time.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.