Wait Until Final Event Javascript Function

By August 25, 2017Blog, Javascript
Allure Web Solutions Code Snippet

This function let’s you wait until a certain javascript event has finished before firing the code inside the function. For example, if you had some function that runs on resizing of the window, you don’t want that function to constantly run every pixel of resizing when someone is making their window smaller. With waitForFinalEvent() function, the function won’t fire than some specified time after the resizing has finished. Cool, right?!

The Function

// Wait for final event
window.waitForFinalEvent = (function () {
    var timers = {};
    return function (callback, ms, uniqueId) {
        if (!uniqueId) {
            uniqueId = "Don't call this twice without a uniqueId";
        if (timers[uniqueId]) {
        timers[uniqueId] = setTimeout(callback, ms);


$(window).resize(function () {
    window.waitForFinalEvent(function () {
        // do something after window resizing has finished
    }, 500, "some unique string");

Credits: https://gist.github.com/nosp4mSnippets/5846729

Mike Doubintchik

Author Mike Doubintchik

More posts by Mike Doubintchik

Leave a Reply