Path: blob/master/6-Selenium/phantomjs/examples/sleepsort.js
164 views
// sleepsort.js - Sort integers from the commandline in a very ridiculous way: leveraging timeouts :P1var system = require('system');23function sleepSort(array, callback) {4var sortedCount = 0,5i, len;6for ( i = 0, len = array.length; i < len; ++i ) {7setTimeout((function(j){8return function() {9console.log(array[j]);10++sortedCount;11(len === sortedCount) && callback();12};13}(i)), array[i]);14}15}1617if ( system.args.length < 2 ) {18console.log("Usage: phantomjs sleepsort.js PUT YOUR INTEGERS HERE SEPARATED BY SPACES");19phantom.exit(1);20} else {21sleepSort(system.args.slice(1), function() {22phantom.exit();23});24}252627