In JavaScript, we fire functions in quasi-parallel:
window.onload=function(){
document.getElementById("test").addEventListener('click', function1(), false);
//consider it takes 3 seconds to be completed
document.getElementById("test").addEventListener('click', function2(), false);
}
How we can fire function2() when the function1() has been completely executed?
In jQuery, we can chain a series of functions as (for example):
$(this).fadeIn(3000).fadeOut(2000);
How to make this change of functions in pure JavaScript?
EDIT: In response to a negative comment and vote, I provide this example:
function delay(time, func){
setTimeout(func,time);
}
delay(2000,function(){alert('Function 1');});
delay(1000,function(){alert('Function 2');});
In this example, you'll first see the alert for "Function 2".
function2is executed afterfunction1's execution has ended. What's your point?setTimeoutwith the parameters reversed..? O_o if you don't want function 2 to execute before function 1, don't set a timeout less than that of function 1. Normally (your first code block) functions execute one after another.function1is completed and thenfunction2is executed. What's executed afterfunction2, is an anonymous function defined infunction1that is executed asynchronously. If you don't understand this, you lack some basic concepts of Javascript.