Ok, I am pretty new to jquery and Javascript. I was reading about callback on w3school and it gives two examples.
Example1:
$("button").click(function(){
$("p").hide("slow",function(){
alert("The paragraph is now hidden");
});
});
Example2:
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
I understand that in first case alert will ONLY be executed after hide() function has finished.
However in second example it is possible that alert might execute before hide finishes.
This has caused some confusion in my understanding. Like for example is it possible that alert('hey') might get executed before alert which comes before it (one with mathmatical calculation) in following case..
$("button").click(function(){
alert(1+2+(3*4)..blah..blah);
alert('hey');
});
OR in this case..
$("button").click(function(){
fn1();
fn2();
});
function fn1(){
for(var i=0;i<100;i++){
$('table').append(blah);
}
}
function fn2(){
alert('hey');
}
Is it possible that 'hey' might appear before fn1 has finished appending? If so do I need to write every thing as callback??
callbackis a function called after finishing some task by givenfunction. So Nah! not all of them are callback.hideis an asynchronous function that takes a callback. Look up those terms.alert(complex calculation);and on the next line it hasalert('hey');I know without a doubt that first alert gets executed first because it appears first in the code. BUT is it not possible that alert('hey') which appears second might get executed even before alert(complex calculation) has finished? Do i need callback there to ensure that 'hey' appears on screen only after complex calculation has been performedWhat is callback?Thank you for your answer it helped.