Re-performing code every X minutes?
February 27, 2010 1:01 PM   Subscribe

Help me do what I think is a simple task in Javascript: re-perform a chunk of code every X minutes.

We've got a custom bit of PHP code on our website that shows what song is playing on our radio station at the moment (i.e. checks the day/time against a schedule, and if a syndicated show is on, it displays it, otherwise it displays the latest song from the playlist category). All well and good.

Now, we're going to add a pop-up option so people can keep our streaming audio up and running and be able to navigate away from the page. I want to put that "now playing" code right below the flash player, but want it to auto-update - that is, re-check every minute and see if the song has changed and if so, update the window. (Because, obviously, nobody is going to think to reload a pop-up window.)

I know nothing about Javascript ... is this easily done? And how?
posted by jbickers to Computers & Internet (7 answers total) 1 user marked this as a favorite
 
Yes, it's easily done. You simply do this:

function foo(){
doSomething;
window.setTimeout('foo()', 60000);
}

window.setTimeout('foo()', 60000);


The setTimeout says to wait a minute (60000 milliseconds), then call the function. Since you want it to do this over and over again, the function itself needs to re-set the timeout.
posted by cerebus19 at 1:11 PM on February 27, 2010


I don't know much javascript, but you can try the setInterval() function.
posted by demiurge at 1:12 PM on February 27, 2010


D'oh! Right, setInterval. I knew there was some other way to do it. The only issue with that is remembering to turn it off if you need to.
posted by cerebus19 at 1:13 PM on February 27, 2010


Use setInterval(). You could also poll a server-side script to check the current song. This would probably require you learning a bit more javascript, but you could have the "now playing" update as soon as a song changes rather than every five seconds or whatever. You can google "Comet" or "long polling" for more detail, but basically you make an AJAX call to your server, which doesn't reply until something happens (e.g a song change).

Of course, there's always your shady friend the iframe - create a simple page that matches your popup's background and just shows your "now playing", and set a refresh metatag on it. I feel a little dirty just suggesting it. But it won't require any javascript.
posted by wonnage at 1:14 PM on February 27, 2010


Tutorial here.
posted by axiom at 1:15 PM on February 27, 2010


Note that nav'ing an IFrame creates click noises in IE.
posted by effugas at 9:01 AM on February 28, 2010


Response by poster: OK, in the hopes that you helpful folks are still reading this ... I think I've come across a fundamental flaw in my approach, and that is that PHP runs on the server while Javascript runs on the client. So I'm wondering if what I'm trying to do is even possible.

To recap, I have a fairly complex bit of PHP code that does a look-up against a number of variables, then queries the Wordpress database, then outputs one of two bits of text: either an item from the look-ups or, if it doesn't match any of those, the most recent WP code from a certain category. As I said, extremely specific to what we do.

So, is it even possible to do all of that within a Javascript script? I've got it in there and it's kinda working - viewing source shows me that the PHP executed and output the correct info - but it's badly formed and the Javascript chokes on it. And even if I get it working, will the auto-refresh ever work?

Thanks again, and open to any alternate suggestions.
posted by jbickers at 3:46 AM on March 3, 2010


« Older Help me verify if this old claim re: greenhouse...   |   he said she didnt Newer »
This thread is closed to new comments.