简体   繁体   English

setTimeout与Java中正常函数执行

[英]setTimeout vs normal function execution in Javascript

In my application, I have a submitSuccesscallback function, which will be triggered from my own JS library when submit was successful. 在我的应用程序中,我有一个submitSuccesscallback函数,当提交成功时,它将从我自己的JS库中触发。 Within submitSuccesscallback , I am first displaying a loader and doing some initialization operations. submitSuccesscallback ,我首先显示一个加载器并进行一些初始化操作。

function submitSuccesscallback(){
  showLoadingIndicator(); // has code a display loader
  doSubmitSuccessOperations();// has code to do some app specific operations
}

here doSubmitSuccessOperations() is taking around 5 secs for complete execution. 在这里doSubmitSuccessOperations()大约需要5秒钟才能完成执行。

Now my problem is above code does n't display loader (ie ui changes from showLoadingIndicator() ) upto 5 secs after I get the submitSuccesscallback() . 现在我的问题是上面的代码在我收到submitSuccesscallback()之后的5秒钟内才不显示加载程序(即ui从showLoadingIndicator()更改submitSuccesscallback()

If I change submitSuccesscallback() like below, I am able to see loader immediately after I trigger submitSuccesscallback(). 如果我像下面那样更改submitSuccesscallback(),则在触发submitSuccesscallback()之后,我能够立即看到加载程序。

 function submitSuccesscallback(){
  showLoadingIndicator(); // has code a display loader
  setTimeout(doSubmitSuccessOperations, 1000);
}

Now what I would like to know is: 现在我想知道的是:

  1. does setTimeout makes my doSubmitSuccessOperations() run in background? setTimeout是否会使我的doSubmitSuccessOperations()在后台运行?

  2. I clearly sense that doSubmitSuccessOperations() is blocking UI operation, is there any concept of UI thread and background thread in JS? 我清楚地感觉到doSubmitSuccessOperations()正在阻止UI操作,JS中是否有UI线程和后台线程的概念?

  3. Any other alternative for setTimeout above? 上面的setTimeout还有其他选择吗?

does setTimeout makes my doSubmitSuccessOperations() run in background? setTimeout是否会使我的doSubmitSuccessOperations()在后台运行?

No. JS is single-threaded. 不会。JS是单线程的。 Code and rendering are in the same thread. 代码渲染位于同一线程中。 That's why long-running operations block rendering. 这就是长时间运行的操作会阻止渲染的原因。

What setTimeout does is set aside that operation until the engine can execute it (it doesn't halt running code) and is at least (and not exactly) after the delay you specified. setTimeout所做的工作被搁置在该操作上,直到引擎可以执行它(它不会停止运行的代码),并且至少(且不完全)在您指定的延迟之后。 Code that comes after it executes normally as if it were the next operation in the thread. 它之后执行的代码正常执行,就好像它是线程中的下一个操作一样。 That means the code inside setTimeout is already not in the same order it appears in your code. 这意味着setTimeout的代码已经与在您的代码中出现的顺序不同。

I clearly sense that doSubmitSuccessOperations() is blocking UI operation, is there any concept of UI thread and background thread in JS? 我清楚地感觉到doSubmitSuccessOperations()正在阻止UI操作,JS中是否有UI线程和后台线程的概念?

Any other alternative for setTimeout above? 上面的setTimeout还有其他选择吗?

Async programming is one, and timers ( setTimeout and friends) are the most available. 异步编程是其中之一,定时器( setTimeout和friends)是最可用的。 In other environments, IE has setImmediate , and Node has process.nextTick . 在其他环境中,IE具有setImmediate ,而Node具有process.nextTick There's also WebWorkers, which are closer to real threads. 还有WebWorkers,它更接近真实线程。 If you have a server, you can use AJAX (which is also a form of async operation) to call a server and let it do the operation for you. 如果您有服务器,则可以使用AJAX(这也是异步操作的一种形式)来调用服务器,并让它为您执行操作。

Here's a video that explains how the event loop works . 这是一段视频,解释了事件循环的工作原理 Somewhere in the middle of the video explains how setTimeout schedules your callbacks . 视频中间的某个地方说明setTimeout如何安排回调

Basically, you have a JavaScript engine that is running your code in the browser. 基本上,您有一个JavaScript引擎在浏览器中运行您的代码。 This engine has a call stack. 该引擎具有调用堆栈。 It's a stack of all the functions that you queued for executing. 它是您排队等待执行的所有功能的堆栈。 There is also a thing called the event loop which is a queue that contain functions that are queued there as a side effect of some event. 还有一个叫做事件循环的事件,它是一个队列,其中包含一些事件的副作用,这些函数在此处排队。 When the call stack is empty, the function that is put on the top of the event loop is pushed in the call stack and get executed. 当调用堆栈为空时,放在事件循环顶部的函数将被推入调用堆栈并得到执行。 This call stack is "inside" your UI thread. 此调用堆栈位于UI线程“内部”。

When you call setTimeout(doSubmitSuccessOperations, 1000); 当您调用setTimeout(doSubmitSuccessOperations,1000); the doSubmitSuccessOperations is added to the event loop 1 second after this line of code is executed. 执行此行代码后1秒钟,会将doSubmitSuccessOperations添加到事件循环。 When all your UI logic is executed(showing spinners, moving texts, animations, etc.), the call stack will be empty. 执行完所有UI逻辑(显示微调器,移动文本,动画等)后,调用堆栈将为空。 Then doSubmitSuccessOperations will be popped out of the event loop and pushed inside the call stack. 然后doSubmitSuccessOperations将弹出事件循环,并推入调用堆栈内部。 This is when the function gets executed. 这是函数执行时的时间。

So, no, setTimeout does not make doSubmitSuccessOperations run in the background. 因此,不,setTimeout不会使doSubmitSuccessOperations在后台运行。 It just make it run after your UI logic. 它只是使其按照您的UI逻辑运行。

There is a concept for background thread and it's called a service worker. 后台线程有一个概念,称为服务工作者。 But you can't do UI operations inside it. 但是您不能在其中执行UI操作。

1) does setTimeout makes my doSubmitSuccessOperations() run in background? 1)setTimeout是否使我的doSubmitSuccessOperations()在后台运行? - No -没有

2) I clearly sense that doSubmitSuccessOperations() is blocking UI operation, is there any concept of UI thread and background thread in JS? 2)我清楚地感觉到doSubmitSuccessOperations()正在阻止UI操作,JS中是否有UI线程和后台线程的概念? - No -没有

3) Any other alternative for setTimeout above? 3)以上setTimeout的其他替代方法? - you can try and put 0 in the timeout, this way the engine will try to execute the function in the first available spot. -您可以尝试将0设置为超时,这样引擎将尝试在第一个可用位置执行该功能。

All of your queries explained in other answer for your third query you can use callback pattern to show loading image it will work you can try Instead of this 您可以在第三个查询的其他答案中解释的所有查询都可以使用回调模式来显示正在加载的图片,可以尝试使用

function submitSuccesscallback(){
    showLoadingIndicator(); // has code a display loader
    doSubmitSuccessOperations();// has code to do some app specific operations
  }

something like 就像是

submitSuccesscallback(function(){
  showLoadingIndicator(function(){
  doSubmitSuccessOperations()
  })
})

And your other function must handle callback something like 而且您的其他函数必须处理类似的回调

function showLoadingIndicator(callback){
// your code to display loading image
 $('loadingimage').show(0,'', function(){
    callback();
});

}

function submitSuccesscallback(callback){
    // your code must be sync here 
    // if asynchrony than call callback in .success function

   //at last(if no asyn operation)
   callback()

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM