Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why setTimeout() with 0 ms. is affect in my function

In my case when hovering <div> so prepend one element inside the <div>

But in some div i want to hide prepended element after hovered element.

This is my code and its work !

HTML :

<div class="one">
    <span class="content"></span>
</div>


<div class="two">
    <span class="content"></span>
</div>

jQuery :

$("div.one > .content").on("hover" , function(){
    var this_ = $(this);
    this_.prev(".top").remove();
});


$("div.two > .content").on("hover" ,function(){
    var this_ = $(this);
    setTimeout(function(){this_.prev(".top").remove();})
});

.one and .two both are same function, But I want to know why second function affect when I using setTimeout() and timing is 0 millisecond , Why 0 millisecond is affect in my function ?

Demo : http://jsfiddle.net/nShCy/

like image 299
l2aelba Avatar asked Apr 15 '26 01:04

l2aelba


1 Answers

It's not 0 ms, it's 4 ms delay.

The answer is simple: mouseenter is fired after hover, so you're trying to remove the element that doesn't exist yet. With a 4ms pause and a hover callback that finishes in less than 4 ms it works as expected.

like image 167
meze Avatar answered Apr 16 '26 17:04

meze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!