繁体   English   中英

等待事件侦听器加载

[英]Wait for event listener load

我有这部分代码,在加载时执行了一些操作。

window.addEventListener("load", () => {
        //Something other
    }, false)

当我执行这个 function

this.searchUnit= function (product, supplier, invoice, status) {
            window.location.href = "units.html";
            this.clean();
            title = document.getElementById("viewTitle");
            title.textContent = "Unità";
            sessionStorage.removeItem("product");
            sessionStorage.removeItem("supplier");
            sessionStorage.removeItem("invoice");
            sessionStorage.removeItem("status");

            //Something other...
        }

我需要事件侦听器 function 的所有操作在执行“this.clean()”之前结束

我怎样才能做到这一点?

如果您的代码中的所有其他事情都是同步的,那么可能会将您想要做的所有事情都放在 setTimeout 中的事件侦听器之后。

setTimeout(functionToBeCalledAfterEventListener,0);

这将确保在执行队列中的所有内容后执行 function。

暂无
暂无

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

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