+ 2
What are callback in JS and why do we need them?
Can anyone explain callbacks with an example plz
4 Réponses
+ 9
L'artisto hamdi Maybe you are doing something wrong. Try rechecking syntax or u can post ur code so that we can help u out.
+ 8
We need callbacks in js as if u have called a function and if it took some time to execute and then your code will move ahead and will not wait for the full execution of your first function. Now, if your second function need some data from ur first function, it will arise an error. Hence, we use callbacks so that a function is called only after the another function has completed its execution.
+ 3
Callbacks are functions which need to be called after a certain work is done and may be with arguments as result of the previous work.
Suppose, you're trying to load some data from the server, may be some Json data, then you'll write a function which will be called once the data is loaded from the server. This function is called a callback function.
Ex.
$.load(url){
//some other methods/properties,
callBackFunc(result){
console.log(result);
} ,
//may be some other methods/properties
}
0
The probleme is even when i use a callback javascript still moves on not waiting for the response of the previous function !