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

  • Home
  • SEARCH
  • 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 7576161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:51:20+00:00 2026-05-30T16:51:20+00:00

I’m searching for a solution where I’m able to run different functions, but some

  • 0

I’m searching for a solution where I’m able to run different functions, but some of them need a timeout and all following functions need to wait until the previous one is finished. Every function should be able to break the complete process.

Now I thought pushing all functions to a stack and loop through them:

function foo() {
    // bar() should wait as long the following is finished:
    setTimeout(function(){
        if ((new Date()).getSeconds() % 2) {
            alert('foo');
            // break loop through functions (bar is not called)
        }
        else {
            // start next function (bar is called)
        }
    }, 1000);
}
function bar() {
    setTimeout(function(){
        alert('bar')
    }, 1000);
}
var functions = new Array('foo', 'bar');
for (var i = 0, length = functions.length; i < length; i++) {
    window[functions[i]]();
}

But how to include wait/break?!

Note: This should work with 2+ functions (amount of functions is changeable)

Note2: I don’t want to use jQuery.

  • 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-30T16:51:21+00:00Added an answer on May 30, 2026 at 4:51 pm

    Note: I have updated my answer, see bottom of post.

    Alright, let’s take a look.

    You’re using the window[func]() method, so you should be able to store and use return values from each function.

    Proof:

    function a(){
        return "value";
    }
    
    var ret_val = window['a']();
    alert(ret_val);
    

    Let’s create a return rule:
    If function returns true, continue execution flow.
    If function returns false, break execution flow.

    function a(){
        //Do stuff
        return (condition);
    }
    
    for(i = 0; i < n; i++){
        var bReturn = window['function']();
        if(!bReturn) break;
    }
    

    Now let’s put it into practice.

    function a(){
        //Do stuff
        return ((new Date()).getSeconds() % 2); //Continue?
    }
    
    function b(){
        //Do stuff
        return true; //Continue?
    }
    
    function c(){
        //Do stuff
        return false; //Continue?
    }
    
    function d(){
        //Do stuff
        return true; //Continue?
    }
    
    var functions = new Array('a', 'b', 'c', 'd');
    
    for (var i = 0; i < functions.length; i++ ) {
        var bReturn = window[functions[i]]();
        if(!bReturn) break;
    }
    

    Depending on when you execute the script, eg, an even or uneven time period, it will only execute function a or execute functions a b & c. In between each function, you can go about your normal business.
    Of course, the conditions probably vary from each individual function in your case.

    Here’s a JSFiddle example where you can see it in action.


    With some small modification, you can for instance, make it so that if function a returns false, it will skip the following function and continue on to the next, or the one after that.

    Changing

    for (var i = 0; i < functions.length; i++ ) {
        var bReturn = window[functions[i]]();
        if(!bReturn) break;
    }
    

    To this

    for (var i = 0; i < functions.length; i++ ) {
        var bReturn = window[functions[i]]();
        if(!bReturn) i++;
    }
    

    Will make it skip one function, every time a function returns false.

    You can try it out here.


    On a side-note, if you were looking for a waiting function that “pauses” the script, you could use this piece of code.

    function pausecomp(millis){
        var date = new Date();
        var curDate = null;
    
        do { 
            curDate = new Date(); 
        }while(curDate-date < millis);
    } 
    

    Update

    After adjusting the code, it now works with setTimeout.

    The idea is that you have an entry point, starting with the first function in the array, and pass along an index parameter of where you currently are in the array and then increment index with one to execute the next function.

    Example | Code

    function next_function(index){
        if(index >= functions.length) return false;
        setTimeout(function(){
                window[functions[index+1]](index+1);
        }, 1000);
    }
    
    function a(index){
        //Do stuff
        if(((new Date()).getSeconds() % 2)) return false; //Stop?
        next_function(index);
    }
    
    function b(index){
        //Do stuff
        if(false) return false; //Stop?
        next_function(index);
    }
    
    function c(index){
        //Do stuff
        if(true) return false; //Stop?
        next_function(index);
    }
    
    function d(index){
        //Do stuff
        if(false) return false; //Stop?
        next_function(index);
    }
    
    var functions = new Array('a', 'b', 'c', 'd');
    
    //entry point   
    window[functions[0]](0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.