LoginSignup
12
9

More than 5 years have passed since last update.

数秒後に実行 function delayedCall()

Last updated at Posted at 2016-08-12

jsで数秒後に処理を実行したい時、

hoge.js

setTimeout(function(){
    console.log("2秒後に実行!");
}, 2000);

のようにsetTimeoutを使うと思いますが、
ミリ秒が直感てきでないし、0を3つ書くのが
めんどくさいなと思っていたところ、
こんな関数を作ってるコードを発見しました。

hoge.js


delayedCall(2,function(){
    console.log("2秒後に実行!");
});


// 数秒後に実行メソッド
function delayedCall(second, callBack){
  setTimeout(callBack, second * 1000);
}

この関数名だと
そんなにタイプ数は変わりませんが、
個人的にお気に入りです!

フレームアニメーションの時とかに使ってます。

hoge.js

$(function(){
    animateTo('intro');
});

// アニメーション管理
function animateTo(frameName) {
  switch (frameName) {
    case 'intro':
      delayedCall(1, function(){
          // なんかアニメ
      });
      animateTo('frame1');
      break;
    case 'frame1':
      delayedCall(1, function(){
          // なんかアニメ
      });
      animateTo('frame2');
      break;
    case 'frame2':
      delayedCall(3, function(){
          // なんかアニメ
      });
      break;
  }
}

// 数秒後に実行メソッド
function delayedCall(second, callBack){
  setTimeout(callBack, second * 1000);
}

ポケモンGO 楽しい

12
9
0

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
12
9