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 8023679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:37:47+00:00 2026-06-04T22:37:47+00:00

Having a nightmare with a function that’s finishing before all the code has run.

  • 0

Having a nightmare with a function that’s finishing before all the code has run. Am trying to build in a counter, and only returning when the code is finished.

I’ve emulated this like so (I know it’s not fantastic, but if someone could point me along the right lines, I’d be very grateful):

//I want this to alert "Done"
alert(timerCheck());

function timerCheck() {
    var finished;
    var myLoop = 5;
    for (i = 0; i < myLoop; i++) {
        //This is emulating the slow code
        window.setTimeout(checkFinished, 900);
        alert(i);
    }
    function checkFinished() {
        //I originally had the "return here, before realising my error
        finished = true;
    }
    if (finished) {
        //This is where my problem is 
        return "done";
    }
}

Like I said, a much simplified example – if someone could point out the mistake, it’d save me a lot of hassle!

  • 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-06-04T22:37:48+00:00Added an answer on June 4, 2026 at 10:37 pm

    You cannot get the return value of a function synchronously if that function calls and depends on asynchronous functions.

    You have to work with callbacks. See this question for some more details.

    For example, your function would look like this:

    // pass a callback which gets the result of function
    timerCheck(function(result) {
        alert(result);
    });
    
    function timerCheck(callback) {
        var myLoop = 5,
            j = 0;
    
        for (var i = 0; i < myLoop; i++) {
            // This is emulating the slow code
            // this will invoke `checkFinished` immediately, 
            // after 900ms, after 1800ms, after 2700ms and after 3600ms
            window.setTimeout(checkFinished, i * 900);
        }
    
        function checkFinished() {
           j++;
           // after checkFinish was called 5 times, we invoke the callback
           if(j === 5) {
               callback('done');
           }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a nightmare, been trying all day with this, and searched google
Having a nightmare trying to figure out event Bubbling.. Full test case : <html>
I'm having a nightmare with the rotation on iPad. I've searched all over the
I am having a nightmare of a time trying to label data with pyplot.
I am having a nightmare trying to debug the event receiver of a Sharepoint
I'm having an asbolute nightmare dealing with an array of numbers which has the
Hi guys just a quick question, After having a nightmare trying to get an
I'm having a bit of a nightmare trying to get REE playing nicely with
I'm having a nightmare with a simple function! I want it to: read the
I'm having a nightmare trying to get some quotes to loop with a delay.

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.