Note that there are some explanatory texts on larger screens.

plurals
  1. POExiting setInterval in Javascript
    primarykey
    data
    text
    <p>I'm trying to create a simple onmouseover animation effect, so that when a user mouses over a div, the relatively positioned div within bounces. However, the code keeps looping and I can't work out why - am I using setInterval() badly? Here it is: </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Bouncer Test Page&lt;/title&gt; &lt;style rel="stylesheet" type="text/css"&gt; div#container{ background-color: #ffffff; width:200px; height: 100px; text-align:center; } div#bouncer{ position:relative; top:50px; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="container"&gt; &lt;div id="bouncer"&gt; &lt;img src="button.jpg"/&gt; &lt;/div&gt; &lt;!-- end of the #bouncer div --&gt; &lt;/div&gt; &lt;!-- end of the #container div --&gt; &lt;script&gt; var selDiv = document.getElementById("bouncer"); var containerDiv = document.getElementById("container"); var index = 10; var intervalHandle1; function popImage(){ selDiv.style.top = relativeHeights[index]; index--; if(selDiv.style.top === '0px'){ index = 0; window.clearInterval(intervalHandle1); dropImage(index, intervalHandle1); } } window.onload = function(){ relativeHeights = ['0px', '5px', '10px', '15px', '20px', '25px', '30px', '35px', '40px', '45px', '50px']; containerDiv.onmouseover = function(){ intervalHandle1 = setInterval(popImage, 50); } } // end of the window.onload anonymous function function dropImage(){ console.log("dropImage was called with index of " + index + ". intervalHandle1 was set to " + intervalHandle1 // insert dropImage code here ); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any suggestions would be much appreciated. </p>
    singulars
    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. This table or related slice is empty.
    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