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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:14:37+00:00 2026-05-30T07:14:37+00:00

I’ve been programming for a few years but I’m new to JavaScript. I’m trying

  • 0

I’ve been programming for a few years but I’m new to JavaScript. I’m trying to create a hacky version of a standard thread ‘notify’ function. I’m running a series of ajax data requests that each take about 200 milliseconds to execute, and I need to wait for them ALL to finish before performing an action (drawing a graph).

My solution was to have each thread increment a global variable when it finished. I then created a timer using setTimeout to check every 100 milliseconds to see if all threads had updated the variable, and if they had, to perform the action.

Despite being a bit hacky, this worked.

Fast-forward a few weeks into the future and my program is growing, and we now need to be
able to have several graphs on the same page, each of which manages its data independently using the aforementioned ajax requests, as well as having several different pages that use the graphs. So I extract the graphing code into a module with require.js, and define a class that performs the graphing to isolate each graph from the others, but suddenly setInterval doesn’t work. I do some Googling and find the following article, which I sort of understand…

http://www.novogeek.com/post/2010/02/08/Scope-problems-with-JavaScript-setInterval-setTimeout-Use-closures!.aspx

So anyway it seems that ‘this’ becomes the window for some strange reason when setInterval is used, and therefore it can’t see my methods.

Anyway, I tried creating a var inst to contain ‘this’ explicitly, which seemed to help, but for some reason it doesn’t like passing ‘inst’ as an argument in the recursive call. The error message from the Firebug console is this:

missing ] after element list
[Break On This Error]
}(9.9, [object Object]))

Here’s a sample from my code:

var inst = this;

// some code... then in a function...
function (){
    inst.waitUntil(10, inst);
}

inst.waitUntil = function(how_many_seconds, inst){
    if (how_many_seconds < 0){
        alert("Script timed out.");
        return;
    } else {
        if (inst.allSchoolsLoaded()){
            setTimeout("("+inst.draw+"("+inst.after+"))", 100);
        } else {
            var argString = "("+inst.waitUntil+"("+(how_many_seconds-0.1)+", "+inst+"))";
            //alert(argString);
            setTimeout(argString, 100);
        }
    }
}

You can assume all the variables mentioned are defined earlier in the class.

I’m at my wits end here, any help would be really appreciated. If anyone can suggest a better solution for the threading issue that avoids setInterval entirely, that would be awesome, otherwise if you can propose a way to get setInterval to work or an alternative function that would be great too.

jQuery is available, and I don’t mind installing other tools if they will help.

Many thanks in advance,

Alex

  • 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-30T07:14:38+00:00Added an answer on May 30, 2026 at 7:14 am

    You are trying to create a string with an object. The standard string representation of an object is [object Object]. You probably will also have problems with inst.waitUntil, as it is converted to the string representation of the function (which is the function’s source in some (most?) browsers).

    Instead of creating a string, just pass a function to setTimeout:

    setTimeout(function() {
        inst.waitUntil(how_many_seconds-0.1, inst);
    }, 100);
    

    Now, inside waitUntil, this will refer to inst. Given this, you might be able to simplify your code further.

    For example:

    this.waitUntil = function(how_many_seconds){
        var self = this;
        if (how_many_seconds < 0){
            alert("Script timed out.");
            return;
        } else {
            var callback = function() {
                self.waitUntil(how_many_seconds-0.1);
            };
    
            if (this.allSchoolsLoaded()){
                callback = function() {
                    self.draw(self.after);
                };
            }
            setTimeout(callback, 100);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
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 want use html5's new tag to play a wav file (currently only supported

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.