Question

I have a paragraph field where I want to put the seconds remaining from this timer:

HTML:

<p>xx seconds remaining</p>

JavaScript:

window.setInterval(retrieveLog, 20000);

How can I do that?

Was it helpful?

Solution

When you call it, store the ending time:

var end = (new Date).getTime() + 20000 * 1000; // current time in ms since 1/1/1970, plus the interval time
window.setInterval(retrieveLog, 20000);

and add another interval that updates the text:

var interval = setInterval(function() {
    var now = (new Date).getTime();
    $("p").text(Math.floor(( end - now ) / 1000));
    if(now > end) clearInterval(interval);
}, 1000);

http://jsfiddle.net/pimvdb/b25KR/

OTHER TIPS

Try this countdown script

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top