Reset and restart javascript countdown (loop) -
i have pure javascript countdown based on date object working fine except wanted countdown keep running.
//the countdown part var d = new date(); var thedate = d.getfullyear() + '-' + (d.getmonth() + 1) + '-' + d.getdate(); var newtime = new date(date.parse(d) + secs * 1000); //var end = new date('02/08/2016 10:00:00'); var end = newtime; var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; if (timer) clearinterval(timer); function showremaining() { var = new date(); var distance = end - now; if (distance < 0) { clearinterval(timer); document.getelementbyid('countdown').innerhtml = 'this callback here should reset , starts again'; 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);
in if (distance < 0)
branch, need remove clearinterval
, reset end
variable.
something this:
if (distance < 0) { d = new date(); thedate = d.getfullyear() + '-' + (d.getmonth() + 1) + '-' + d.getdate(); newtime = new date(date.parse(d) + secs * 1000); end = newtime; return; }
Comments
Post a Comment