Synchronous delay in code execution

前端 未结 10 1185
长情又很酷
长情又很酷 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 03:11

    Using the new Atomics API, you can start synchronous delays without performance spikes:

    const sleep = milliseconds => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds)
    
    sleep(5000) // Sleep for 5 seconds
    
    console.log("Executed after 5 seconds!")
    

提交回复
热议问题