How do I write a blocking synchronous method in Javascript?

前端 未结 3 1386
情书的邮戳
情书的邮戳 2020-12-31 15:25

I\'m trying to mock out a method which takes a long time for testing purposes but can\'t find a good way to do this in Javascript. Any good approaches besides writing a very

3条回答
  •  一个人的身影
    2020-12-31 15:29

    How about a loop that checks time?

    function sleep(milliSeconds){
        var startTime = new Date().getTime();                    // get the current time
        while (new Date().getTime() < startTime + milliSeconds); // hog cpu until time's up
    }
    

提交回复
热议问题