Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
REMitchell
GitHub Repository: REMitchell/python-crawling
Path: blob/master/6-Selenium/phantomjs/examples/sleepsort.js
164 views
1
// sleepsort.js - Sort integers from the commandline in a very ridiculous way: leveraging timeouts :P
2
var system = require('system');
3
4
function sleepSort(array, callback) {
5
var sortedCount = 0,
6
i, len;
7
for ( i = 0, len = array.length; i < len; ++i ) {
8
setTimeout((function(j){
9
return function() {
10
console.log(array[j]);
11
++sortedCount;
12
(len === sortedCount) && callback();
13
};
14
}(i)), array[i]);
15
}
16
}
17
18
if ( system.args.length < 2 ) {
19
console.log("Usage: phantomjs sleepsort.js PUT YOUR INTEGERS HERE SEPARATED BY SPACES");
20
phantom.exit(1);
21
} else {
22
sleepSort(system.args.slice(1), function() {
23
phantom.exit();
24
});
25
}
26
27