I have a setTimeout that adds an eventListener, but if an action occur i want to remove the eventListener.
let to;
function() fun{
to = setTimeout(() => el.addEventListener('mouseover', foo), 500);
}
fun();
windows.addEventListener('mouseup', function(removeListener){
clearTimeout(to);
el.removeEventListener('mouseover', foo)
})
I remove the event listener because the code from the timeout could have already started and still to proceed attaching the event before the clear, but is there a case where they collide and the addEventListener line is still executed after the remove line. So my question is does the setTimeout and the addEventListener code happen on the same thread and is there a way they execute at the same time?
Another example is if I have 2 settimeouts both happening after 500ms, how do I know that the removing of the listener will happen after the adding.
setTimeout(() => el.addEventListener('mouseover', foo), 500);
setTimeout(() => el.removeEventListener('mouseover', foo), 500);