Wednesday, May 28, 2014

jQuery Callback Functions

1) JavaScript statements are executed line by line.

2)  However, with effects, the next line of code can be run even though the effect is not finished.

3) This can create errors.

4) To prevent this, you can create a callback function.

Typical syntax: $(selector).hide(speed,callback);

5) function that will be executed after the hide effect is completed

$("button").click(function(){
  $("p").hide("slow",function(){
    alert("The paragraph is now hidden");
  });
});

6) The example below has no callback parameter, and the alert box will be displayed before the hide effect is completed

$("button").click(function(){
  $("p").hide(1000);
  alert("The paragraph is now hidden");
});



No comments:

Post a Comment