JSFiddle - React, Tailwind, and code Playground

非同期関数

by rough cheap

JavaScript

function syncAfter3seconds () {
    var start = new Date().getTime();
    
    while(true) {
        var current = new Date().getTime();
        if ((current - start) >= 3000) {
            break;
        }
    }
    
    var end = current;
    console.log('## Sync RESULT ##');
    console.log('start :' + start);
    console.log('end   :' + end);
    console.log('result:' + (end - start));
    console.log();
}

function asyncAfter3seconds () {
    var start = new Date().getTime();
    setTimeout(function() {
        var end = new Date().getTime();
        console.log('## Async RESULT ##');
        console.log('start :' + start);
        console.log('end   :' + end);
        console.log('result:' + (end - start));
        console.log();
    }, 3000);
}

var ts = new Date().getTime();
syncAfter3seconds();
asyncAfter3seconds();
var te = new Date().getTime();
console.log("## TOTAL TIME ##");
console.log("total :" + (te - ts));
console.log("## TOTAL TIME ##");