views:

462

answers:

2

Hi everyone,

In Javascript, I want my onmouseout event to sleep/pause/wait/ (not sure of the proper terminology here) for three seconds before taking effect. How is that accomplished?

thanks

+3  A: 
function outfunction(event) {
    var that = this; // to be able to use this later.
    window.setTimeout(function() {
        …
    /* you can use 'that' here to refer to the element
       event is also available in this scope */
    }, 3000);
}
Georg
You ought to pass the event object to out, and to the inner anonymous function.
Triptych
one cannot pass it to the inner function but it works because the anonymous function uses the scope of the outer function.
Georg
Accessing the event object in the inner function is dubious, better would be to acquire any info needed from the event object into variables in the outer function.
AnthonyWJones
But so is using 'that', and I know of no way to circumvent that, besides using another anonymous function.
Georg
+1  A: 
var doSomething = function () {
    //Some code will here after 3 seconds of mouseout
};

anElement.onmouseout = function () {
   setTimeout(doSomething, 3000);
};

What the above code does is execute the doSomething function after 3 seconds of the onmouseout being invoked

Andreas Grech