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?

20th Nov 2019, 12:59 PM
Sona Sarkisian
Sona Sarkisian - avatar
3 Respuestas
+ 4
var timeout = setTimeout (leaveFunct,3000) element.onmouseenter = function(){ clearTimeout (timeout) }
20th Nov 2019, 3:01 PM
Kevin ★
+ 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
20th Nov 2019, 3:10 PM
Ryan
Ryan - avatar
+ 2
Xianort , Kevin Star , thank you very much!
21st Nov 2019, 5:30 AM
Sona Sarkisian
Sona Sarkisian - avatar