Delay a section of code for 15 seconds

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Delay a section of code for 15 seconds

Post 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. :)
loCode
Forum Newbie
Posts: 9
Joined: Fri Oct 08, 2010 2:24 am

Re: Delay a section of code for 15 seconds

Post 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.
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Re: Delay a section of code for 15 seconds

Post 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.
loCode
Forum Newbie
Posts: 9
Joined: Fri Oct 08, 2010 2:24 am

Re: Delay a section of code for 15 seconds

Post 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.
Post Reply