How to call an AJAX function after every 15 seconds?
I have called a JavaScript function on<body onload>
event where there is a while loop which will iterate unless and until the desired response is fetched from a PHP page.
However since the loop will make a very f开发者_JAVA技巧requent set of requests, that is causing my browser to hang.
Is there another easy way where we can perform AJAX function request evry 15 minutes without causing much load on the browser?
use setInterval that calls your function every 15 min. something like this:
setInterval(function(){
// Call to your function that performs an ajax call...
}, 15 * 60 * 1000);
Here is link to setInterval documentation: http://www.w3schools.com/jsref/met_win_setinterval.asp
You could use a jquery plugin for polling.
https://launchpad.net/jquery.poll
var ajax_call = function () {
$.ajax(options);
};
setInterval(ajax_call, interval);
to clear it use
clearInterval(ajax_call);
Weel i think you can use setTimeout() like this
setTimeout(yourajaxfunction,90000);
// 900000 is the delay in millisecond, after that your function is called.
inside your loop so that the function is executed only when the timeout ends.
P.S.
i think ajax was born to be "responsive", you should refresh things responding to an action of the user and not with a timeout in my opinion! :)
精彩评论