Skip to content

Instantly share code, notes, and snippets.

@tomoh1r
Last active December 12, 2015 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoh1r/4711625 to your computer and use it in GitHub Desktop.
Save tomoh1r/4711625 to your computer and use it in GitHub Desktop.
1. 「0」を出力する 2. 1秒待つ 3. 「1」を出力する 4. 1秒待つ 5. 「2」を出力する see http://techblog.yahoo.co.jp/programming/js_callback/
'use strict';
(function(seq) {
var hoge = function(seq) {
// timeout の秒数 (ms)
var timeout = 1 * 1000;
// 与えられた配列の length が 0 なら終わり
if (seq.length == 0) return;
// 与えられた配列の length が 1 なら次は処理しないので終わり
console.log(seq[0]);
if (seq.length == 1) return;
// 1秒後に次の処理を実施する。
setTimeout(function() {
// 与えられた配列の頭をとった後ろで処理をする
hoge([].slice.call(seq, 1));
}, timeout);
};
hoge(seq);
})([0, 1, 2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment