Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Please check this</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; var intervals = []; var counters = { "shoes":{"id":"shoe1","minutes":1,"seconds":5}, "trousers":{"id":"trouser1","minutes":10,"seconds":0} }; // generate this on the server and note there is no comma after the last item window.onload = function() { for (var el in counters) { countdown(counters[el]) }; } function countdown(element) { intervals[element.id] = setInterval(function() { var el = document.getElementById(element.id); var minutes = element.minutes; var seconds = element.seconds; if(seconds == 0) { if(minutes == 0) { el.innerHTML = "countdown's over!"; clearInterval(intervals[element.id]); return; } else { minutes--; seconds = 60; } } if(minutes &gt; 0) { var minute_text = minutes + (minutes &gt; 1 ? ' minutes' : ' minute'); } else { var minute_text = ''; } var second_text = seconds &gt; 1 ? 'seconds' : 'second'; el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining'; seconds--; element.seconds = seconds; element.minutes = minutes; }, 1000); } &lt;/script&gt; shoes: &lt;span id="shoe1"&gt;&lt;/span&gt;&lt;br /&gt; trousers: &lt;span id="trouser1"&gt;&lt;/span&gt;&lt;br /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Working solution is <a href="http://jsfiddle.net/arunpjohny/Lwxhx/" rel="nofollow">here</a>.</p> <p>EDIT: There was a minor issue with the script. I've fixed it.</p> <p>After recalculating the <code>seconds</code> and <code>minutes</code> you have to set the new values back in the <code>element</code> object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload