Synchronous delay in code execution

前端 未结 10 1199
长情又很酷
长情又很酷 2020-12-03 02:29

I have a code which needs to be executed after some delay say 5000 ms.Currently I am using setTimeout but it is asynchronous and i want the execution to wait for its return.

10条回答
  •  天涯浪人
    2020-12-03 02:59

    JavaScript is a single-threaded language. You cannot combine setTimeout and synchronous processing. What will happen is, the timer will lapse, but then the JS engine will wait to process the results until the current script completes.

    If you want synchronous methods, just call the method directly!

    If you want to process something after the setTimeout, include it or call it from the timeout function.

提交回复
热议问题