phantomjs/examples/sleepsort.js

24 lines
696 B
JavaScript
Raw Normal View History

// sleepsort.js - Sort integers from the commandline in a very ridiculous way: leveraging timeouts :P
function sleepSort(array, callback) {
var sortedCount = 0,
i, len;
for ( i = 0, len = array.length; i < len; ++i ) {
2011-06-16 17:48:41 +04:00
setTimeout((function(j){
return function() {
2011-06-16 17:48:41 +04:00
console.log(array[j]);
++sortedCount;
(len === sortedCount) && callback();
};
2011-06-16 17:48:41 +04:00
}(i)), array[i]);
}
}
if ( phantom.args < 1 ) {
console.log("Usage: phantomjs sleepsort.js PUT YOUR INTEGERS HERE SEPARATED BY SPACES");
phantom.exit();
} else {
sleepSort(phantom.args, function() {
phantom.exit();
});
}