Does JavaScript have a sort of event listeners?
My scenario:
I want to execute Inc function twice with different parameters, first execution of Inc and then the second execution, but the second execution must be executed after X seconds (settimeout…) after the first Inc function is completed (all it’s iterations, as soon as clearInterval ).
So I need sort of event and a listener (of-course it must be asynchronous).
What can you suggest? (please no jQuery…)
function Inc(val1,val2){
var intervalID = setInterval(function(){
if (val1 > val2) clearInterval(intervalID);
val1= val1 + 10;
}, 400);
}
Edit: Well, since I don’t know why you don’t want it to know about the continuation, here’s another suggestion with the exact same semantics but maybe a desirable syntax, now there is a simulation of event delegation anyway 🙂 (thing is, you need the Inc function to somehow report completion):
Previous: Something like this should to the trick (I did not test it though):
Then call it like this: