简体   繁体   中英

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

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

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(functionToBeCalledAfterEventListener,0);

This will make sure to execute the function after all the things in queue are executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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