0
How to stop a running function in JS?
The function called on mouseleave, has got setTimeout 3000. If the cursor goes over the same element before 3s have passed, the function continues working. How to prevent it with the mouseenter event?
3 Answers
+ 4
var timeout = setTimeout (leaveFunct,3000)
element.onmouseenter = function(){
clearTimeout (timeout)
}
+ 3
You can use a combination of setTimeout() and clearTimeout() to achieve this affect.
The setTimeout() method returns a unique ID that can be used to clear timer created by the setTimeout() via clearTimeout().
The returnedĀ IDĀ is a positive integer value which identifies the timer created by the call toĀ setTimeout() this value can be passed toĀ clearTimeout()Ā to cancel the timeout.
Here's an example code:
https://code.sololearn.com/WNYe6SgxoviZ/?ref=app
+ 2
Xianort , Kevin Star , thank you very much!