Answer the question
In order to leave comments, you need to log in
Why does the callback fire earlier than it should?
Wrote the code https://codepen.io/CitizenOne/pen/qxZmoP
I'm trying to make the callback always run after the main function code. Callback should sort of wait.
What am I doing wrong? I, as in all articles on this topic, pass a function as an argument.
Answer the question
In order to leave comments, you need to log in
Callback should sort of wait.
function callBack(func2){
setTimeout(function(){
console.log(1);
func2();
}, 500);
}
callBack(function(){
console.log(2);
});
function callBack(func2){
console.log(1);
func2();
}
callBack(function(){
console.log(2);
});
And what's the problem, everything works exactly as you asked.
The callBack function sets a timer and calls the function passed to it as a parameter, 2 is output. After 500 milliseconds, the function passed in the timeout is called, 1 is output.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question