LoginSignup
1
1

More than 5 years have passed since last update.

[JavaScript] jQueryのメソッドにCallback関数を適応させる時のメモ

Last updated at Posted at 2015-01-08

大前提:JavaScriptとjQueryのコードが混合している場合、
    JavaScriptのコードが実行されてからjQueryのメソッドが実行される
    コールバック関数は、処理終わってから実行される。


// main
$("#hoge").click(function () {
    console.log("function main start");

    // 適当なメソッド
    console.log("method animate start");
    var value;
    $("#foo").animate({
        right: 100
    }, 1000 ,callback(value)
    );
    console.log("method animate end");

    console.log("function main end"); 
});

// callback関数
var callback = (function(value)  {
    console.log("function callback start");
    if (value !== "undefined") {
        console.log("condition true");
    }
    console.log("function callback end");
});

実行される順番
"function main start"
 "method animate start"
  "function callback start"
   "condition true"
  "function callback end"
 "method animate end"
"function main end"

やりたかったことと何か違う…

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1