Jump to content

timed delay


bran

Recommended Posts

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.
[code]
$start=time();
$i=1;
while ($i<=10) {
     $now=time();
     if ($now>$start+5) {;
          $i++;
     }
}[/code]
This doesn't do anything, but hopefully it illustrates what I want, and maybe where I'm heading in the wrong direction.
Link to comment
Share on other sites

[code]<?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;
     }

?>[/code]

that was no fun to make, but.. yeah.. that will do it.. for any amount of SECONDS, up to 999 [=
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

[!--quoteo(post=354892:date=Mar 14 2006, 09:25 AM:name=bran)--][div class=\'quotetop\']QUOTE(bran @ Mar 14 2006, 09:25 AM) [snapback]354892[/snapback][/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.
Link to comment
Share on other sites

[!--quoteo(post=355248:date=Mar 15 2006, 02:28 AM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 15 2006, 02:28 AM) [snapback]355248[/snapback][/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.
Link to comment
Share on other sites

[!--quoteo(post=355371:date=Mar 15 2006, 03:14 PM:name=bran)--][div class=\'quotetop\']QUOTE(bran @ Mar 15 2006, 03:14 PM) [snapback]355371[/snapback][/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 ... [=
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.