javascript set interval run as separate thread?

前端 未结 3 713
野趣味
野趣味 2021-02-13 14:41

I want to use a timer as a fallback in case I end up in an infinite loop. It seems that set interval is the right way to do this. However, it\'s not working for me.

Fro

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-13 15:04

    zerkms has the correct answer. But I would add that web workers are a way to get some multi-threaded-ish behavior from client side javascript.

    var worker = new Worker('my_task.js');
    worker.onmessage = function(event) {
     console.log("Called back by the worker!\n");
    };
    

    The worker runs in a background thread, and you can exchange messages and subscribe to events. It's pretty nifty.

提交回复
热议问题