Create a gist now

Instantly share code, notes, and snippets.

@el-gringo /sleep.js
Last active Dec 25, 2015

What would you like to do?
I use the multithread HTML5 worker which will be able to abort an synchronous XMLHttpRequest pointing to an unresponsive URL. This does not block the browser.
function sleep(milliseconds) {
var xhr = new XMLHttpRequest();
// Open a non existing url'GET', location.protocol + '//sleep' + (new Date()).getTime(), false);
var blob = new Blob([
"onmessage = function() { setTimeout(function() {postMessage('done');}, "+milliseconds+"); }"]);
var blobURL = window.URL.createObjectURL(blob);
var worker = new Worker(blobURL);
worker.onmessage = function() {
var start = new Date().getTime();
var end = new Date().getTime();
console.log('time: ' + (end - start));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment