Page 1 of 1

Delay a section of code for 15 seconds

Posted: Fri Oct 08, 2010 2:16 am
by Smudly
Hi,

I have some queries that update some statistics in a table I have. I need these queries to run only after 15 seconds have passed however. This way if the user leaves before the 15 seconds, the queries won't run. I've tried using Sleep, however it seems to just stop all the code all together for the allotted time. There must be a way to do this. :)

Re: Delay a section of code for 15 seconds

Posted: Fri Oct 08, 2010 2:29 am
by loCode
A nice way to do this is to make your update code (PHP) a separate class function that can be included and called in any PHP page. Then use AJAX to call your PHP class function using setInterval("my_ajax_code()",15*1000);, this will be a nice solution without stopping code or anything.

Re: Delay a section of code for 15 seconds

Posted: Fri Oct 08, 2010 10:20 am
by Smudly
Alright, I've been messin with this for a while and came across this code:

Code: Select all

<script type="text/javascript">
function delayQueries()
{
  timeoutID = window.setTimeout(doQueries, 2000);
}

function doQueries()
{
  var runQuery = "<?php phpQuery(); ?>";    
}
</script>
In the doQueries function I am trying to run the phpQuery function. How do I do this inside javascript? If I can get this part to work, the rest should be easy.

Re: Delay a section of code for 15 seconds

Posted: Fri Oct 08, 2010 5:47 pm
by loCode
It'd be nice if JavaScript and PHP had such a relationship, but it doesn't. You need to use AJAX.

Try http://www.w3schools.com/ajax/tryit.asp ... ajax_first for example code.