Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have some scope issues. E.g.</p> <pre><code>function startClock() { minute = this.minute; element = this.element; minute = checkTime(minute); document.getElementById(element).innerHTML = minute; minute++; this.minute = minute; t=setTimeout(function(){startClock()},1000); this.counter = t; } </code></pre> <p>This will declare <code>minute</code>, <code>element</code> and <code>t</code> in global scope, thus every call to <code>startClock</code> will overwrite these values.</p> <p>Here is an refactored version:</p> <pre><code>function Clock(element) { this.element = element; this.minute = 0; this.toggle = true; this.active = false; } Clock.prototype = { startClock: function() { this.minute = this.checkTime(this.minute); this.element.innerHTML = this.minute; this.minute++; var that = this; this.counter = setTimeout(function(){that.startClock()},1000); }, checkTime = function(i) { if (i&lt;10) { i="0" + i; } return i; }, stopClock: function() { this.element.innerHTML = this.minute; clearTimeout(this.counter); }, clearClock: function() { this.element.innerHTML= "start"; this.element = ""; this.minute = 0; this.toggle = true; this.counter = null; } } function initClock(ele) { value = document.getElementById(ele).innerHTML; if (typeof window[ele] == 'undefined') { window[ele] = new Clock(document.getElementById(ele)); } if (value == "start" || window[ele].active == false) { window[ele].toggle = true; } else {window[ele].toggle = false;} if (window[ele].toggle) { window[ele].toggle = false; window[ele].active = true; if (value == "start") { window[ele].minute = 0; } window[ele].startClock(); } else { window[ele].toggle = true; window[ele].active = false; window[ele].stopClock(); } } </code></pre> <p>Read more about <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Objects" rel="noreferrer"><em>Objects in JavaScript</em></a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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