Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:32:18+00:00 2026-05-24T06:32:18+00:00

Is it possible to chain setTimout functions to ensure they run after one another?

  • 0

Is it possible to chain setTimout functions to ensure they run after one another?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:32:20+00:00Added an answer on May 24, 2026 at 6:32 am

    Three separate approaches listed here:

    1. Manually nest setTimeout() callbacks.
    2. Use a chainable timer object.
    3. Wrap setTimeout() in a promise and chain promises.

    Manually Nest setTimeout callbacks

    Of course. When the first one fires, just set the next one.

    setTimeout(function() {
        // do something
        setTimeout(function() {
            // do second thing
        }, 1000);
    }, 1000);
    

    Chainable Timer Object

    You can also make yourself a little utility object that will let you literally chain things which would let you chain calls like this:

    delay(fn1, 400).delay(fn2, 500).delay(fn3, 800);
    
    function delay(fn, t) {
        // private instance variables
        var queue = [], self, timer;
        
        function schedule(fn, t) {
            timer = setTimeout(function() {
                timer = null;
                fn();
                if (queue.length) {
                    var item = queue.shift();
                    schedule(item.fn, item.t);
                }
            }, t);            
        }
        self = {
            delay: function(fn, t) {
                // if already queuing things or running a timer, 
                //   then just add to the queue
            	  if (queue.length || timer) {
                    queue.push({fn: fn, t: t});
                } else {
                    // no queue or timer yet, so schedule the timer
                    schedule(fn, t);
                }
                return self;
            },
            cancel: function() {
                clearTimeout(timer);
                queue = [];
                return self;
            }
        };
        return self.delay(fn, t);
    }
    
    function log(args) {
        var str = "";
        for (var i = 0; i < arguments.length; i++) {
            if (typeof arguments[i] === "object") {
                str += JSON.stringify(arguments[i]);
            } else {
                str += arguments[i];
            }
        }
        var div = document.createElement("div");
        div.innerHTML = str;
        var target = log.id ? document.getElementById(log.id) : document.body;
        target.appendChild(div);
    }
    
    
    function log1() {
    	  log("Message 1");
    }
    function log2() {
    	  log("Message 2");
    }
    function log3() {
    	  log("Message 3");
    }
    
    var d = delay(log1, 500)
        .delay(log2, 700)
        .delay(log3, 600)

    Wrap setTimeout in a Promise and Chain Promises

    Or, since it’s now the age of promises in ES6+, here’s similar code using promises where we let the promise infrastructure do the queuing and sequencing for us. You can end up with a usage like this:

    Promise.delay(fn1, 500).delay(fn2, 700).delay(fn3, 600);
    

    Here’s the code behind that:

    // utility function for returning a promise that resolves after a delay
    function delay(t) {
        return new Promise(function (resolve) {
            setTimeout(resolve, t);
        });
    }
    
    Promise.delay = function (fn, t) {
        // fn is an optional argument
        if (!t) {
            t = fn;
            fn = function () {};
        }
        return delay(t).then(fn);
    }
    
    Promise.prototype.delay = function (fn, t) {
        // return chained promise
        return this.then(function () {
            return Promise.delay(fn, t);
        });
    
    }
    
    function log(args) {
        var str = "";
        for (var i = 0; i < arguments.length; i++) {
            if (typeof arguments[i] === "object") {
                str += JSON.stringify(arguments[i]);
            } else {
                str += arguments[i];
            }
        }
        var div = document.createElement("div");
        div.innerHTML = str;
        var target = log.id ? document.getElementById(log.id) : document.body;
        target.appendChild(div);
    }
    
    function log1() {
        log("Message 1");
    }
    
    function log2() {
        log("Message 2");
    }
    
    function log3() {
        log("Message 3");
    }
    
    Promise.delay(log1, 500).delay(log2, 700).delay(log3, 600);

    The functions you supply to this version can either by synchonrous or asynchronous (returning a promise).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible in C# to connect one event to another so emitting first
is it possible in rails 2.3.X to start a new chain of commands after
Is it possible in jQuery to daisy-chain selectors like so? var sData = $('#myTableRow
Does someone knows if it's possible to dynamically create a call chain and invoke
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Is it possible to chain multiple proxies in a single request using cURL? For
Possible Duplicate: How can I chain implicits in Scala? Can Scala apply multiple implicit
Is it possible to simulate a servlet filter chain using @ApplicationPath and @Path annotations
I want to chain async rest service calls and have single callback when they
Is it possible to chain together DOM manipulation code so that it adds to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.