Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中的另一个线程中运行代码_Javascript_Multithreading_Settimeout - Fatal编程技术网

在javascript中的另一个线程中运行代码

在javascript中的另一个线程中运行代码,javascript,multithreading,settimeout,Javascript,Multithreading,Settimeout,我想分离页面上的线程以防止gui冻结。为此,我正在运行一个函数,该函数将在另一个线程中使用setTimeout冻结gui,但仍然冻结 代码和jsbin链接如下: <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></scri

我想分离页面上的线程以防止gui冻结。为此,我正在运行一个函数,该函数将在另一个线程中使用setTimeout冻结gui,但仍然冻结

代码和jsbin链接如下:

<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"
  type="text/javascript"></script>
  <meta charset="utf-8" />
</head>
<body>
  <div id="div1"></div>
  <div id="div2"></div>
  <input type="button" value="düðme" id="btn" />

  <script type="text/javascript">
      $("#btn").on("click",function(){
        $("#div1").html(new Date());
      });

      $(document).ready(function(){
        setTimeout(function() { count(); },1);
      });

      function count(){
        for(var i =0;i<100000;i++){
          $("#div2").html(i);
        }
          $("#div2").append(new Date());
      }
  </script>

</body>
</html>

$(“#btn”)。在(“单击”,函数(){
$(“#div1”).html(新日期());
});
$(文档).ready(函数(){
setTimeout(函数(){count();},1);
});
函数计数(){
for(var i=0;ijavascript(browser)是一个单线程应用程序,因此即使您在任何时间点使用setTimeout,也只有一个线程在运行(执行脚本执行、重新绘制ui等)


由于脚本每毫秒运行一次,它将冻结线程,从而阻塞UI

Javascript不是多线程的,因此您可能希望查看即使您已通过
setTimeout
委托执行,它仍将在同一个线程中执行,它将只等待队列中的时间,并将停止任何其他活动,直到完成为止

请参阅《JS忍者的秘密》一书中的这张精彩图片:

您可以使用“承诺”操作异步功能:

承诺是创建承诺时不一定知道的值的代理。它允许您将处理程序与异步操作的最终成功值或失败原因关联。这使异步方法可以像同步方法一样返回值:异步方法不会立即返回最终值,而是返回承诺在将来某个时候提供价值

第1步:

const dosomethingPromise = (data, otherInput) => {
return new Promise((resolve, reject) => {
     /* do your work here*/
    resolve(true) /* return result here or you can use reject for execute catch block*/
})
})

第二步: 在代码中这样使用它:

 Promise.resolve(dosomethingPromise(data,otherInput))
        .then((result) => {
            /*your result come here*/
            console.log("Progress finished=>", result);
        }, (error) => {
            console.log(error)
        })
        .catch(console.log);

您还可以使用和“all”方法而不是“resolve”方法在一行中执行多个任务并获得最终结果。

这仍然将在同一个线程中执行。所有操作都将在同一个线程中执行。唯一的例外是WebWorkers。但是如果我们说
async:true
,jquery ajax请求在不同的线程中工作。我现在正在搜索有关WebWorkers的好信息。我需要d在其他线程中运行一些东西,同时gui仍然可以触摸并从网络加载另一个数据。该ajax请求的回调仍将在同一线程中执行。
async:true
只意味着浏览器可以在等待ajax调用结果时花一些时间处理其他任务。那么async/wait方法呢?它们的用途是什么?我确信异步作业应该在与执行它的线程不同的线程上运行,例如在桌面/android应用程序中,我们可以在后台执行无阻塞UI任务,并在异步作业完成时更新UI。那么,在Javascript/Typescript中使用这些方法的目的是什么,以及为什么它们是called异步?