mercredi 9 septembre 2020

Is there any difference how to pass function to setTimeout method?

I found very strange thing. I am using Jquery and javascript in my work. I need to remove element from DOM by using Jquery`s method for ex. $el.remove();.

I have to remove it after 1 second but there is very strange difference between method calls.

If I do this:

setTimeout(() => $el.remove(), 1000);

It works fine and the element is removed after 1 sec. BUT if I do this:

setTimeout($el.remove.bind($el, $el), 1000);

OR

setTimeout($el.remove, 1000);

It just doesn`t work. Through debugger I know that the remove method gets executed. But the element is not removed from DOM. So my question "Why is doesn't work if I pass method reference or by using bind?"

I am using JQuery 1.9.1




Aucun commentaire:

Enregistrer un commentaire