javascript - Counter not live -


my following script works: 1 problem: doesnt count down. gives right output (time untill date) doesnt update automatically.

anyone nows whats wrong?

the main thing countdown timer works server time (thats why post, , time.php).

        <script> var end = new date('10/7/2013 7:0 pm');  var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer;  var now; function startshowremaining(strnow) { = new date(strnow); showremaining(); } function showremaining() { //var = new date(strnow);     var distance = end - now;     if (distance < 0) {          clearinterval(timer);         document.getelementbyid('countdown').innerhtml = 'expired!';          return;     }     var days = math.floor(distance / _day);     var hours = math.floor((distance % _day) / _hour);     var minutes = math.floor((distance % _hour) / _minute);     var seconds = math.floor((distance % _minute) / _second);      document.getelementbyid('countdown').innerhtml = days + 'days ';     document.getelementbyid('countdown').innerhtml += hours + 'hrs ';     document.getelementbyid('countdown').innerhtml += minutes + 'mins ';     document.getelementbyid('countdown').innerhtml += seconds + 'secs'; }  timer = setinterval(showremaining, 1000);  function postrequest(strurl) { var xmlhttp; if (window.xmlhttprequest) { // mozilla, safari, ...     var xmlhttp = new xmlhttprequest(); } else if (window.activexobject) { // ie     var xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.open('post', strurl, true); xmlhttp.setrequestheader('content-type',     'application/x-www-form-urlencoded'); xmlhttp.onreadystatechange = function() {     if (xmlhttp.readystate == 4) {         startshowremaining(xmlhttp.responsetext);     } } xmlhttp.send(strurl); } postrequest('time.php');       </script> <div id="countdown"></div> 

time.php:

print date("m/d/y, g:i:s"); 

the problem polling server time once. call showremaining again , again, now time never change, therefor output never change.

if need use server time, need send request sending in postrequest everytime showremaining called , use now:

some of post deleted since wrong

you can find example in this jsfiddle, replaced contents of postrequest stub use client time instead of making ajax call (since don't have access time.php).

of course, repeated server requests quite inefficient, should consider using client's clock. consider using server time once , use javascript clock add time.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -