Skip to content
Advertisement

how to delay an insertBefore?

It is possible to stop an insertBefore, inside an addEventListener, to add a smooth css, so that the movement produced by the insertion of the div is not abrupt for the user?

I have read many questions, i have tried using settimeout in various ways, without success:

JavaScript
JavaScript
JavaScript

Advertisement

Answer

this refers to a different context inside your setTimeout callback; it doesn’t refer to the element for which the event is dispatched anymore.

There are a few ways you could do this, here are 3:

Use an arrow function, where this doesn’t get bound to a new context:

JavaScript

Store a reference to this for use inside the callback:

JavaScript

Manually bind the current context to the callback function:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement