简体   繁体   English

等待事件侦听器加载

[英]Wait for event listener load

I have this portion of code with some action executed on load.我有这部分代码,在加载时执行了一些操作。

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

When I execute this function当我执行这个 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...
        }

I need all the operation of event Listener function ended before execute "this.clean()"我需要事件侦听器 function 的所有操作在执行“this.clean()”之前结束

How can I do this?我怎样才能做到这一点?

If every other thing is synchronous in your code then probably put all the things that you want to do after the event listener in a setTimeout.如果您的代码中的所有其他事情都是同步的,那么可能会将您想要做的所有事情都放在 setTimeout 中的事件侦听器之后。

setTimeout(functionToBeCalledAfterEventListener,0);

This will make sure to execute the function after all the things in queue are executed.这将确保在执行队列中的所有内容后执行 function。

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

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