Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A few pointers:</p> <p>1) Since you are trying to access a dom element, it is better to check if the element is present in the DOM.</p> <p>Since you are already using jQuery, it is advisable to have all this code inside <code>$(document).ready()</code>.</p> <p>2) <code>clearTimeout</code> removes the reference <code>t</code> in this case from the memory and any later reference to it will result in unpredictable results. The variable <code>t</code> is actually used in case you want to clear off the <code>timeout</code> from an element at some point.</p> <p>3) Passing the value <code>timedCount()</code> in string form implies JS will have to apply an eval to get its value, which in this case refers to a function. <a href="http://www.jslint.org" rel="nofollow">http://www.jslint.org</a>. JS standards ask to avoid eval uses. It is better to use an anonymous function here, that in turn calls the desired function <code>timedCount()</code>.</p> <p>4) As far as another implementation goes, it really depends on how and when you want your <code>stopCount()</code> function to be called. In your implementation, it will actually never be called as it keeps calling the same function after a span of 1 sec.</p> <p>The desired code could be something like</p> <pre><code>function timedCount() { // the first 2 lines doing something t = setTimeout(function() { // if clear time out logic if ( can_clear_timeout() ) { stopCount(); } else { return timedCount(); } }, 1000); } </code></pre> <p>5) Another warning is one should not use global variables like <code>timer_is_on</code> as it is against the standards.</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