Note that there are some explanatory texts on larger screens.

plurals
  1. POsetTimeout in methods of multiple objects of the same type
    text
    copied!<p>I need help with the use of "setTimeout" in the methods of the objects of the same type. I use this code to initiate my objects:</p> <pre><code> function myObject(param){ this.content = document.createElement('div'); this.content.style.opacity = 0; this.content.innerHTML = param; document.body.appendChild(this.content); this.show = function(){ if(this.content.style.opacity &lt; 1){ this.content.style.opacity = (parseFloat(this.content.style.opacity) + 0.1).toFixed(1); that = this; setTimeout(function(){that.show();},100); } } this.hide = function(){ if(this.content.style.opacity &gt; 0){ this.content.style.opacity = (parseFloat(this.content.style.opacity) - 0.1).toFixed(1); that = this; setTimeout(function(){that.hide();},100); } } } </code></pre> <p>Somewhere I have 2 objects:</p> <pre><code> obj1 = new myObject('Something here'); obj2 = new myObject('Something else here'); </code></pre> <p>Somewhere in the HTML code I use them:</p> <pre><code> &lt;button onclick="obj1.show()"&gt;Something here&lt;/button&gt; &lt;button onclick="obj2.show()"&gt;Something else here&lt;/button&gt; </code></pre> <p>When the user presses one button, everything goes OK, but if the user presses one button and after a short time interval he presses the other one, the action triggered by the first button stops and only the action of the second button is executed. I understand that the global variable "that" becomes the refence of the second object, but I don't know how to create an automatic mechanism that wouldn't block the previously called methods.</p> <p>Thank you in advance and sorry for my English if I made some mistakes :P</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