javascript - What happens to code after history.back()? - Stack Overflow

I have code like the following:window.history.back();myFunction(10);Is history.back() a blockingnon

I have code like the following:

window.history.back();
myFunction(10);
  • Is history.back() a blocking / non-blocking call?

  • Is there an assurance that myFunction() will be executed? or will not be executed?

  • Is this a possible race condition where history.back() happens asynchronously and whether myFunction() is called depends on timing of uncontrollable events?

I have code like the following:

window.history.back();
myFunction(10);
  • Is history.back() a blocking / non-blocking call?

  • Is there an assurance that myFunction() will be executed? or will not be executed?

  • Is this a possible race condition where history.back() happens asynchronously and whether myFunction() is called depends on timing of uncontrollable events?

Share Improve this question asked Aug 28, 2014 at 7:25 teddbyteeteddbytee 1031 silver badge5 bronze badges 4
  • Interesting question, but what are your experiences on the matter? Have you tried this yourself? – Peter Commented Aug 28, 2014 at 7:29
  • 2 This is a good question! Reading the spec, myFunction could be called if the BeforeUnloadEvent cancels the navigation at the very least – CodingIntrigue Commented Aug 28, 2014 at 7:30
  • @Peter The function runs; however i want to be sure. – teddbytee Commented Aug 28, 2014 at 7:35
  • @teddbytee, sure : good question, but your own experiences on the matter are always interesting as extra info – Peter Commented Aug 28, 2014 at 7:36
Add a ment  | 

1 Answer 1

Reset to default 9

The spec says that history.back queues a task.

The actual history manipulation code (which is internal to the JS implementation) will therefore be executed during the next run of the main event loop. Your call to myFunction executes synchronously in the current round of execution, so it will always be executed in pliant environments.

However, only synchronous code in myFunction is guaranteed to execute. Consider this example:

function myFunction() {
  console.log('synchronous');
  debugger

  setTimeout(function() {
    console.log('async');
    debugger
  })
}

window.history.back();
myFunction();

The first debugger statement is always hit. The second, which is deferred to a future event loop tick using setTimeout, will not.

This article is a good explanation of the JavaScript event loop

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742295488a4417029.html

相关推荐

  • javascript - What happens to code after history.back()? - Stack Overflow

    I have code like the following:window.history.back();myFunction(10);Is history.back() a blockingnon

    20小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信