Created
January 14, 2015 02:55
-
-
Save tjbrennan/7a8098dbe76a4c0adc3d to your computer and use it in GitHub Desktop.
js sleepsort
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
var array = [4, 8, 1, 3, 2, 6, 2, 7, 20, 1, 6, 34, 3, 5, 0, 9]; | |
function setTimeoutSort (array, spread) { | |
var sorted = []; | |
var i; | |
for (i = 0; i < array.length; i++) { | |
(function () { | |
var el = array[i]; | |
setTimeout(function() { | |
sorted.push(el); | |
if (sorted.length === array.length) { | |
console.log(sorted); | |
} | |
}, el * spread); | |
})(); | |
} | |
} | |
setTimeoutSort(array, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment