I know this isn’t supposed to be inline, but YUI library’s dialogs force me to. My issue is that whenever I hover over this div, the margin-left scroll activated but it does not stop when I move the mouse out of the div. The JS console reports that:
Uncaught ReferenceError: timerID is not defined
And here’s the code:
<div class="span1" onmouseover="
var timerID;
$(document).ready(function(){
timerID = setInterval(scrollLeft, 10);
function scrollLeft(){
$('.inner_wrapper').animate({
marginLeft: '-=30px'
});
}
});
" onmouseout="clearInterval(timerID)">
</div>
EDIT: The thing is that I can NOT run SCRIPT tags inside dialogs (they are already created via scripts, which filter any javascript besides inline one like onmouseover and onmouseout). So your suggestions of encapsulating the onmouseover and onmouseout handles in a single function will not work in this case.
It’s a scope problem. You variable
timerIDis not visible in onmouseout.Generally it is a good idea, to put stuff into a function instead of clobbering it all into the attributes. This avoids all these scope-problems. And it’s an even better idea to use handlers instead of the
on-...-atrributes.Define your function outside the
onmouseoverattribute and call another function in themouseoutwhich clears it.Something like this (to avoid nasty global varaibles)
and then
or even better, bind the handlers directly via:
As of new jQuery 1.7,
.on()should be preferred.