恰巧code什么history.back()后? [英] What happens to code after history.back()?

查看:149
本文介绍了恰巧code什么history.back()后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code这样的:

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


  • history.back()阻塞/非阻塞电话?

    是否有 myFunction的()将被执行的保证?或将不会执行?

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

    这是一个可能的竞争状态,其中 history.back()异步发生,
    是否 myFunction的()被称为依赖于不可控事件的时机?

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

    推荐答案

    规范说, history.back 队列任务

    实际操作历史code(这是内部的JS实现)的主要的事件循环。您的来电 myFunction的在本轮执行同步执行,所以它总是顺应环境中执行。

    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 compliant environments.

    然而,只有在 myFunction的同步code是保证执行。考虑这个例子:

    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();
    

    第一个调试器语句总是击中。第二,这是使用推迟到未来事件循环剔的setTimeout ,不会的。

    本文是一个很好的JavaScript的事件循环的解释

    This article is a good explanation of the JavaScript event loop

    这篇关于恰巧code什么history.back()后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆