Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Rather than finding the bug in your code, perhaps it would be useful to discuss how you could find it yourself. The first step is to open your devtools window (F12) and open the console (press the little icon that looks like a greater than sign with three horizontal bars to the right). When you run your program you are likely to see the following:</p> <pre><code>TypeError: Cannot set property 'zIndex' of undefined </code></pre> <p>The debugger will stop at the line</p> <pre><code>setTimeout(function() {this.style.zIndex='0';},600); </code></pre> <p>Hmmm. Apparently <code>this.style</code> is undefined. How could that be? Go into the console (which is in the context in which the error occurred, in other words, inside the function you passed to setTimeout) and type</p> <pre><code>this.style </code></pre> <p>and indeed you will see that it is undefined. Why is that? Well, what is <code>this</code>?</p> <pre><code>this </code></pre> <p>and you will see something like</p> <pre><code>Window {top: Window, window: Window, location: Location, ...} </code></pre> <p>Why would <code>this</code> be the window instead of what I think it should be? Well, <code>this</code> in general is set to <code>window</code> for a function called without an explicit <code>this</code>, as in <code>elt.set_style()</code>. That's what's happening here.</p> <p>How can I set <code>this</code> within a function that is called by the system, such as the one passed to <code>setTimeout</code>? You can do what @Satpal recommended, and store the value of <code>this</code> outside the function under another name such as <code>self</code>, and explicitly refer to that. Or, you could bind the function to <code>this</code>, as in</p> <pre><code>function timeout_func(){ this.style.zIndex=0; } setTimeout(timeout_func.bind(this)); </code></pre> <p>In addition to using the console to see error messages and type in things to be evaluated, you'll find the "scope variables" and "watch expressions" windows very useful.</p> <p>In general, problems such as this can be solved easily using devtools. If you don't know what it is, Google Chrome devtools (start off by hitting F12). Not using this is literally like programming in the dark.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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