I have a jQuery function that creates an element when the user hovers on a element. When the mouse goes away from the element I set a timeout to destroy it. While creating and normally destroying the element does work flawlessly, the remove
inside the timeout does not work and dies silently. I suspect there is some problem in the interaction between window.setTimeout
and jQuery.remove
, but couldn't find anything on the web.
My code:
//Element creation
function createElement(){
$(".my_class").remove()
content = compose_content() //It's an <ul>
$('<div></div>', {
class : "my_class",
html: content
}).insertAfter($("#first_row"))
}
//Element destruction
function setDestroyTimeout(){
if(perform_some_checks()){
window.setTimeout(function(){
console.log("Removing...")
$(".my_class").remove()
}, 1000)
}
}
$(".another_class").hover(createElement(), setDestroyTimeout())
The first remove (the one in the createElement
function) works smoothly, while the second doesn't do anything. The Timeout works as well, I checked with the console.log
instruction.