squiblo Posted August 31, 2010 Share Posted August 31, 2010 Hello, what I want to do is call a function every X seconds, I can do this when staying on 1 page without navigating off, but my problem is that I want to call the function every X seconds even if the user is moving from page to page and I do not know how this can be done. For example I want to call the function every 20 seconds, I am on the homepage for 10 seconds, then move to the help page, then I only have to wait another 10 seconds before the function is called. Any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/212188-do-something-every-x-seconds/ Share on other sites More sharing options...
shlumph Posted August 31, 2010 Share Posted August 31, 2010 If you can, build it in such a way that a cron would work. Since HTTP is stateless, this would be reasonable. Or, a far less reliable way, would be to make use of AJAX calls. Quote Link to comment https://forums.phpfreaks.com/topic/212188-do-something-every-x-seconds/#findComment-1105674 Share on other sites More sharing options...
squiblo Posted August 31, 2010 Author Share Posted August 31, 2010 Unfortunately I do not know how to make a cron, so I looked for a generator, that only went down to minutes not seconds I'm not having much luck Quote Link to comment https://forums.phpfreaks.com/topic/212188-do-something-every-x-seconds/#findComment-1105676 Share on other sites More sharing options...
Maq Posted August 31, 2010 Share Posted August 31, 2010 There may be a better way but you can use a combination these two lines in a recursive manner to achieve what you want. t=setTimeout("yourFunction()", 20000); and clearTimeout(t); Quote Link to comment https://forums.phpfreaks.com/topic/212188-do-something-every-x-seconds/#findComment-1105699 Share on other sites More sharing options...
Alex Posted August 31, 2010 Share Posted August 31, 2010 You can use setInterval() and store the last time the function was called in a cookie to transfer the time between pages. Quote Link to comment https://forums.phpfreaks.com/topic/212188-do-something-every-x-seconds/#findComment-1105711 Share on other sites More sharing options...
.josh Posted September 5, 2010 Share Posted September 5, 2010 In general, 20s is not a long time...is there a reason it can't just be reset on page load? It would be far easier for you to just start the process over on a new page load...my first suggestion is for you to reconsider whether it's really necessary to carry over a timeout of that short of time from page to page. if it is something you absolutely need to do, then as AlexWD mentioned, I'd also store the last call time in a cookie and draw from that from page to page. Quote Link to comment https://forums.phpfreaks.com/topic/212188-do-something-every-x-seconds/#findComment-1107553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.