js 中的 sleep 方法, 阻塞式


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())


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM