Last active
March 25, 2017 04:31
-
-
Save hrgui/a0397194a40b7962c2895b30514b388f to your computer and use it in GitHub Desktop.
Timeout test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script src="node_modules/setImmediate/setImmediate.js"></script> | |
<script src="ajax-test.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ajax(url) { | |
return new Promise((resolve, reject) => { | |
var xmlhttp = new XMLHttpRequest(); | |
xmlhttp.onreadystatechange = function () { | |
if (xmlhttp.readyState == XMLHttpRequest.DONE) { | |
if (xmlhttp.status == 200) { | |
resolve(xmlhttp.responseText); | |
} | |
else if (xmlhttp.status == 400) { | |
reject(xmlhttp.responseText); | |
} | |
else { | |
reject(xmlhttp.responseText); | |
} | |
} | |
}; | |
xmlhttp.open("GET", url, true); | |
xmlhttp.send(); | |
}); | |
} | |
let gotUser = false; | |
let timedout = false; | |
let start = new Date(); | |
ajax('https://randomuser.me/api') | |
.then(user => { | |
user = JSON.parse(user); | |
console.log('fetch returns', user, timedout); | |
if (timedout) { | |
document.body.innerText = new Date() - start; | |
return; | |
} | |
gotUser = true; | |
user.duration = new Date() - start; | |
document.body.innerText = JSON.stringify(user); | |
}) | |
function busyLoop() { | |
let date = new Date(); | |
let future = new Date(date.getTime() + 2000); | |
while (date < future) { | |
date = new Date(); | |
} | |
} | |
setTimeout(_ => busyLoop(), 4); | |
setTimeout(function () { | |
console.log('timer fired from setTimeout'); | |
}, 200); | |
function setTimerImmediate(func, duration) { | |
let date = new Date(); | |
let future = new Date(date.getTime() + duration); | |
function loop() { | |
setImmediate(function () { | |
date = new Date(); | |
if (date >= future) { | |
func(); | |
} else { | |
loop(); | |
} | |
}); | |
} | |
loop(); | |
} | |
setTimerImmediate(function () { | |
console.log('timer fired from setTimerImmediate, last call!'); | |
console.time('timer1'); | |
console.time('timer2'); | |
//delay between the function call and timeout is ~2s + 1 | |
setTimeout(function() { | |
console.log('am i called first?') | |
console.timeEnd('timer1'); | |
}, 1); | |
setImmediate(function() { | |
console.timeEnd('timer2') | |
console.log('fire off timeout') | |
!gotUser && document.write('timedout'); | |
timedout = true; | |
}); | |
}, 200); | |
setTimerImmediate(function() { | |
console.log('start busy loop again'); | |
busyLoop(); | |
}, 201); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "setimmediate-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"setimmediate": "^1.0.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment