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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:55:52+00:00 2026-06-08T12:55:52+00:00

I have this following piece of code: var stats = { …., checkExistance :

  • 0

I have this following piece of code:

var stats = {
....,
checkExistance :
function(url){
    var newUrl = url.substring(0, url.lastIndexOf("/")) + "/asyncCheckChartExistance";
    var xhrObj = stats.getXhr();

    var poolInterval = setInterval("poll()", 100);

    function poll(){
        xhrObj.open("GET", newUrl, true);
        xhrObj.send(null);

        xhrObj.onreadystatechange = function(){
            if(xhrObj.readyState === 4 && xhrObj.status === 200){
                if (xhrObj.responseText.length === true){
                    console.log("Exists!");
                    clearInterval(poolInterval);
                } else {
                    console.log("Not Yet!");
                }
            }
        }
    }
},
}

I created the stats namespace. In this namespace I’m trying to create a function which polls the server every second. I should access this function this way: stats.checkExistance(myUrl).

However it seems that the setInterval function is not able to see the poll() function. I know that this is normal behavior taking in consideration that these are nested inside another function.

If I were to write this in the Global namespace there would be no problem but I’m interested to make this work in this kind of namespace. Any ideas? Thanks!

  • 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-08T12:55:54+00:00Added an answer on June 8, 2026 at 12:55 pm

    when you pass a string to setInterval, it runs in the global scope, by default, where poll would not be defined since it only exists in the scope of the checkExistance function.

    To fix the issue, pass an anonymous function to setInterval instead:

    var poolInterval = setInterval(function () {
        poll();
    }, 100);
    

    Passing an anonymous function is usually the best idea as it allows you to write any javascript expressions/statements for the interval instead of just calling one function.

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

Sidebar

Related Questions

I have the following piece of code from a book. There is this function
I have the following piece of code: function checkAmount() { var PayField = document.getElementById('paymentamount');
I have the following piece of code: jQuery.fn.shake = function(intShakes, intDistance, intDuration) { this.each(function()
Say you have the following piece of code: function someProcess() { var deferred =
So I have the following piece of code: var structures = { loginStructure :
We have the following piece of code (idea for this code was found on
I have the following piece of Mootools 1.11 code (not upgradable as this is
I have this following piece of code: public TimestampedRowStorage GetTimestampedRowStorage(string startTime, string endTime, long
I have following piece of code: private void nameTextBox_Leave(object sender, EventArgs e) { var
I have defined the following piece of Coffeescript code below, which defines a function

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.