What I am trying to achieve:
I have a button. When this button is clicked - it fires a function which sets setInterval to run another function once a second. This function should move a divider 100 pixels to the left.
What is actually occurring:
The divider moves 100 pixels to the left but then stops and does not move on any subsequent firing on the setInterval function.
The image which fires the initial function:
<img id="playerimg" src="../../Downloads/1375889362_toggle-right_red.png" alt=""
width="42" height="42" border="0" onclick="vidgalshow()"/>
The statement inside this function which initializes and fires the Interval:
closemaindiv = setInterval("collapser()", 1000);
And finally the collapser function itself which should perform the animation:
document.getElementById("maindiv").style.position = "absolute";
document.getElementById("maindiv").style.left = (document.getElementById("maindiv").style.left - 100);
I know that the Interval is firing every second because I have an alert() running each time. (But the box moves once to the left and then not again(!)).
Any ideas on what's going on here?
left
takes a length not an integer. – Quentin Aug 7 at 14:09setInterval(collapser, 1000)
. If you pass a string of script, it is executed in the global scope (and treated like aneval
call, so it has those implications)...so ifcollapser
were a local function, it wouldn't be found and would throw an exception. Just a general convention to follow – Ian Aug 7 at 14:12