Skip to content

Instantly share code, notes, and snippets.

@adamay000
Last active December 21, 2015 13:37
Show Gist options
  • Save adamay000/4d25d6b277a2ef686a3b to your computer and use it in GitHub Desktop.
Save adamay000/4d25d6b277a2ef686a3b to your computer and use it in GitHub Desktop.
実行タイミング理解してるかチェック
var zero = +new Date()
, now = function(){ return +new Date() - zero; }
, wait = function(time){ var start = now(); while(now() - start < time){}; };
console.log('start:', now()); // #start: 0
// ========================================
setTimeout(function(){
console.log('async:', now()); // #async: ?
}, 1000);
wait(1500);
wait(500);
// ========================================
console.log('end:', + now()); // #end: 2000
// #asyncと#end、先に実行されるのはどっち?
// #asyncで表示されるおおまかな数値は?
var zero = +new Date()
, now = function(){ return +new Date() - zero; }
, wait = function(time){ var start = now(); while(now() - start < time){}; };
console.log('start:', now()); // #start: 0
// ========================================
setTimeout(function(){
console.log('asyncA:', now()); // #asyncA: ?
}, 1000);
setTimeout(function(){
console.log('asyncB:', now()); // #asyncB: ?
}, 500);
wait(2000);
// ========================================
console.log('end:', + now()); // #end: 2000
// #asyncAと#asyncB、先に実行されるのはどっち?
// #asyncAと#asyncB、それぞれで表示されるおおまかな数値は?
var zero = +new Date()
, now = function(){ return +new Date() - zero; };
console.log('start:', now()); // #start: 0
// ========================================
setTimeout(function(){
console.log('asyncA:', now()); // #asyncA: ?
}, 1001);
setTimeout(function(){
console.log('asyncB:', now()); // #asyncB: ?
}, 1000);
wait(2000);
// ========================================
console.log('end:', + now()); // #end: 2000
// #asyncAと#asyncB、先に実行されるのはどっち?
var zero = +new Date()
, now = function(){ return +new Date() - zero; }
, wait = function(time){ var start = now(); while(now() - start < time){}; };
console.log('start:', now()); // #start: 0
// ========================================
setTimeout(function(){
console.log('asyncA:', now()); // #asyncA: ?
setTimeout(function(){
console.log('asyncA-2:', now()); // #asyncA-2: ?
}, 250);
}, 1000);
setTimeout(function(){
console.log('asyncB:', now()); // #asyncB: ?
setTimeout(function(){
console.log('asyncB-2:', now()); // #asyncB-2: ?
}, 500);
}, 500);
wait(2000); // #wait
// ========================================
console.log('end:', + now()); // #end: 2000
// #asyncA, #asyncB, #asyncA-2, #asyncB-2、実行される順番は?
// #asyncA-2と#asyncB-2、それぞれで表示されるおおまかな数値は?
// #waitをコメントアウトした場合に、#asyncA, #asyncB, #asyncA-2, #asyncB-2、実行される順番は?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment