js 中的 sleep 方法, 阻塞式
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], factory)
} else if (typeof exports === 'object') {
factory(exports)
if (typeof module === 'object' && module !== null) {
module.exports = exports = exports.sleep
}
} else {
factory(window)
}
}(this, function (exports) {
exports.sleep = function sleep(ms) {
var start = new Date().getTime()
while (new Date().getTime() < start + ms);
}
}));
console.log(1, new Date().toLocaleString())
sleep(2000)
console.log(2, new Date().toLocaleString())