tags:

views:

92

answers:

4

I have divs that I want to display at specific times throughout the day. I have it working in PHP, but it requires refreshing the browser manually. I would like my script to automatically load the right div when the time is right.

Am I on the right track? Perhaps there is a jquery plugin for this sort of thing that would handle the refreshing?

Any help is greatly appreciated... Thanks!

<?php

$time = date("H\:i");

if (($time > "16:59") && ($time < "18:59")) {
    echo "<div>1</div>";
}

elseif (($time > "18:59") && ($time < "20:59")) {
    echo "<div>2</div>"; 
}

elseif (($time > "20:59") && ($time < "22:59")) {
    echo "<div>3</div>";    
}

 else {
    echo "<div id='out'><p>Outside the specified point in time.</p></div>";
}

?>
A: 

I would use JQuery timer to poll every so often; when the time is right, use a $.load() to load in new information into the div on the page. You should do the time checks in Javascript, as you've done, so that you're not constantly transferring needless data between client and server.

Jasie
thanks for the answer, jasie! Can you give me an example of how I would go about adding that to my code?
dot
@dot - RTFM !!!!!!! (and some more parantheses to make 10 characters :D)
Sune Rasmussen
Otherwise, there's of course jQuery Ajaxify: http://plugins.jquery.com/project/Ajaxify
Sune Rasmussen
I've haven't done much with jquery and it's very confusing to me. Thanks for responding though! :)
dot
A: 

Your backend should return a specific bit of html you want to display.

Your front-end should just be polling one specific url on the backend that returns this information.

(There are other ways to do it but this is my opinion)

djFire
+2  A: 

You won't need a jquery plugin to handle refreshing, you can just create a timer which checks every few minutes/seconds

@jasie was right to mention timers. He mentioned jquery timer, but you can just as well use regular javascript timers, like this (you tagged jquery, so I'll use jquery):

var timer = setInterval(function(){
    var hour = (new Date()).getHours();

    if (hour >= 17 && hour < 19) {
        var $div = $('<div>1</div>');
    } else if (hour >= 19 && hour < 21) {
        var $div = $('<div>2</div>');
    } else if (hour >= 21 && hour < 23) {
        var $div = $('<div>3</div>');
    } else {
        var $div = $("<div id='out'><p>Outside the specified point in time.</p></div>");
    }
    $div.appendTo('body');
}, 2000); // checking every 2 seconds.

That should do the trick

arnorhs
Thanks! that kind of works. But now it appends the content instead of replacing the content... it adds a new div every 2 seconds... Any ideas?
dot
*sigh* http://www.google.com/search?q=jquery+replace+html You'd better give arnorhs another point for doing your job for you!!
sequoia mcdowell
@dot replacing what content? The whole content of BODY ? If so:$('body').html($div);Maybe you'll want to take a look at:http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery
arnorhs
A: 

As a start, using jQuery's AJAX call and a simple js timer. If page has one div with ID of "showithere", this willre-load your PHP page every 10 minutes:

<head>
<script>
​function fn () {
  alert ('now');
  $("#showithere").load ("http://jsbin.com/help/");
}
</script>
</head>

<body onLoad="setTimeout (fn, 600000);" >

​​​​​​​​​​​​​​​

Ed Schembor