Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You just need to take the <code>minutes</code> and <code>seconds</code> variable declarations out of the inner function, like this:</p> <pre><code>function countdown(element) { var minutes = element.minutes; var seconds = element.seconds; intervals[element.id] = setInterval(function() { var el = document.getElementById(element.id); 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--; }, 1000); } </code></pre> <p>When you call <code>countdown</code>, you want to fetch the initial values for each countdown and then slowly decrease them (closure makes sure that the values will stay available as long as the anonymous function requires them). What you were doing before was reset the countdown values at the start of each tick, so the countdown never had a chance to... well, count down.</p> <p><strong>Update:</strong></p> <p>If you need to update the values inside <code>window.counters</code> while the countdowns are active (although I don't see why you would want to do that; if you want to do anything meaningful with the "current" countdown values, just do it inside the anonymous function), you can simply add this at the end:</p> <pre><code>var second_text = seconds &gt; 1 ? 'seconds' : 'second'; el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining'; seconds--; // ADD THIS: element.minutes = minutes; element.seconds = seconds; </code></pre>
 

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