HI guys, I need to be able to make an ajax call to be made after every few minutes. Basically the ajax call would be to check on new emails in an inbox. If there are new emails it would 开发者_开发知识库download the emails to a database. I got all the server side code set up fine. I just need to know how do I set up on the front end the part where the ajax call is made after every few minutes plus it should be set up such that we don't end up with parallel ajax calls being made i.e if the ajax call hasn't returned a response it shouldn't start a new ajax request.
http://www.w3schools.com/jsref/met_win_settimeout.asp
or
http://www.w3schools.com/jsref/met_win_setinterval.asp
set interval is probably more like what you after, you can then call something like
setInterval(function(){ /*code you want to run here */ }, 100);
then your code will run every 100 milliseconds.
You will obviusly need to use a much larger time, something like 60000 for every minute. If you use the settimeout
method then you can have it check wait for the response of the ajax, and let the code the handles the ajax response set a new timeout.
There is also Prototype periodical update that makes things a lot easier
few examples
http://www.tutorialspoint.com/prototype/prototype_ajax_periodicalupdater.htm
http://www.prototypejs.org/learn/introduction-to-ajax
http://www.prototypejs.org/api/ajax/periodicalUpdater
I would set up a cookie that bounces back and forth between the server and the client to store the last time an ajax request was made. Then on each page load, a js script reads the cookie and sets a setTimeout
for the remaining time before the next ajax request.
精彩评论