让浏览器阻塞10秒钟的方法

//这个东西不会睡是十秒,因为异步执行了

console.log("sleep start")

setTimeout(function(){

},10000)

console.log("sleep over")

正确的阻塞10秒的方法是下边这样的

    function sleep(milliSeconds){
        console.log("sleeping")
        function _getTime(){
            return new Date().getTime();
        }
        var start = _getTime();
        while(_getTime()<start+milliSeconds);
    }
   sleep(10000)

这个玩意,放在哪里都会睡着10秒绝对堵着

 

posted @ 2017-02-15 16:15  白与小寒  阅读(509)  评论(0编辑  收藏  举报