Jump to content


Photo

timed delay


  • Please log in to reply
10 replies to this topic

#1 bran

bran
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationSavannah, GA

Posted 14 March 2006 - 12:31 AM

Ok, I'm pretty new to all of this, but I feel like I've got a good grasp of the basics. Here's what I'm trying to do:

I want to create a loop that will pause for a certain number of seconds at the end, before it runs the next iteration. I've been trying to do some stuff with the time() function, but nothing that is producing any results.

Below is something along the lines of what I'm messing with. I save the atart time as a variable, and then I'm trying to make the loop continue only if a certain number of seconds have passed. If I can just come up with a loop that will print the current time every 5 seconds, I can adapt it as needed.
$start=time();
$i=1;
while ($i<=10) {
     $now=time();
     if ($now>$start+5) {;
          $i++;
     }
}
This doesn't do anything, but hopefully it illustrates what I want, and maybe where I'm heading in the wrong direction.
There are 10 kinds of people in the world, those who understand binary, and those who do not.

#2 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 14 March 2006 - 01:55 AM

<?php
 
 $TIMEOUT_TIME = 5;
 
 $start = time();
 $start = substr($start, strlen($start) - 3);

 $end = $start + $TIMEOUT_TIME;

 $loop = false;
     while (!$loop) {
        $time = time();
        $time  = substr($time, strlen($time) - 3);
        if ($time == $end) $loop = true;
     }
 
?>

that was no fun to make, but.. yeah.. that will do it.. for any amount of SECONDS, up to 999 [=

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 March 2006 - 02:05 AM

You can also use the sleep function:

[a href=\"http://www.php.net/sleep\" target=\"_blank\"]http://www.php.net/sleep[/a]

Keep in mind that unless you have altered the php.ini file to extend execution time, it is limited to 30 seconds.

#4 bran

bran
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationSavannah, GA

Posted 14 March 2006 - 12:34 PM

I think either of those solutions will work for me. Thanks for the help ya'll!

The sleep function will probably do it more simply, but I want to understand the other solution, too. I have one main question:

1) I don't understand the use of the substr function. I looked at the manual on php.net, but I'm not grasping the application. Can you explain what it's doing in this particular application?
There are 10 kinds of people in the world, those who understand binary, and those who do not.

#5 bran

bran
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationSavannah, GA

Posted 14 March 2006 - 02:25 PM

Okay, so both of these suggestions create a delay. But they are delaying the entire page to load.

Here's what I'm doing, more specifically.

I'm selecting a random sound byte from a databse and playing it. That part works fine. I want to loop it so that when the sound byte ends, it will select and play another. I was expecting to use this delay funtion to do so.

I basically have 3 parts to my code. The first part selects the file to be played and prints the name of the clip. The second part generates an iframe and plays the sound in it. The last part should be the delay before it loops back to part one.

Well, part one is printing, but part two isn't executing until after part three has done. Is there maybe a way I can force it to generate the frame before proceeding?

Also I am running into a problem with the maximum execution time of 30 seconds, but I am going to try to raise it. Is that a bad idea?
There are 10 kinds of people in the world, those who understand binary, and those who do not.

#6 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 14 March 2006 - 10:02 PM

[!--quoteo(post=354892:date=Mar 14 2006, 09:25 AM:name=bran)--][div class=\'quotetop\']QUOTE(bran @ Mar 14 2006, 09:25 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I basically have 3 parts to my code. The first part selects the file to be played and prints the name of the clip. The second part generates an iframe and plays the sound in it. The last part should be the delay before it loops back to part one.
[/quote]

Have your page refresh after a certain amount of time...then it will begin the process again, and you won't have to worry about max execution time.

Use the meta refresh and set it for a second longer than your music file and have it refresh back to itself.

#7 bran

bran
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationSavannah, GA

Posted 15 March 2006 - 04:16 AM

That did the trick. Hopefully with some experience I can come up with something a little more sphisticated, but this works. Thanks for the help.

There are 10 kinds of people in the world, those who understand binary, and those who do not.

#8 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 07:28 AM

How would you like to make it more sophisticated?

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#9 bran

bran
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationSavannah, GA

Posted 15 March 2006 - 03:14 PM

[!--quoteo(post=355248:date=Mar 15 2006, 02:28 AM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 15 2006, 02:28 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
How would you like to make it more sophisticated?
[/quote]

Well, I looked at the mp3 player on my sister's MySpace page, for instance. That actually got me to thinking that maybe I should be using Flash instead of just PHP (PHP can be used in Flash, can't it?). I downloaded it and looked at the code, but it is way beyond the basics I have gotten into so far.

With the version I've come up with so far, it has to refresh the page to pull a new song, and it has to play in an iframe. I think those are both weaknesses that will hamper me adding some of my wish list features later on. I ultimately want to have a member rating system that affects a weighted random song selection. I'm not far enough into it to know if the refreshing will affect that, but for now I am pondering how to pause and restart. Skipping songs was easy, just a button that refreshes the page. But even if you pause, the timer on that meta refresh is still ticking.
There are 10 kinds of people in the world, those who understand binary, and those who do not.

#10 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 09:02 PM

[!--quoteo(post=355371:date=Mar 15 2006, 03:14 PM:name=bran)--][div class=\'quotetop\']QUOTE(bran @ Mar 15 2006, 03:14 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, I looked at the mp3 player on my sister's MySpace page, for instance. That actually got me to thinking that maybe I should be using Flash instead of just PHP (PHP can be used in Flash, can't it?). I downloaded it and looked at the code, but it is way beyond the basics I have gotten into so far.

With the version I've come up with so far, it has to refresh the page to pull a new song, and it has to play in an iframe. I think those are both weaknesses that will hamper me adding some of my wish list features later on. I ultimately want to have a member rating system that affects a weighted random song selection. I'm not far enough into it to know if the refreshing will affect that, but for now I am pondering how to pause and restart. Skipping songs was easy, just a button that refreshes the page. But even if you pause, the timer on that meta refresh is still ticking.
[/quote]

Take a look at pandora.com ... [=

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#11 bran

bran
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationSavannah, GA

Posted 15 March 2006 - 10:30 PM

Well. LOL. Wasn't looking for quite that fancy, but it definitely makes me think I need Flash to do anything very complex.
There are 10 kinds of people in the world, those who understand binary, and those who do not.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users