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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:24:17+00:00 2026-06-01T22:24:17+00:00

I’m making a simple script reads out the nginx server status in node.js I

  • 0

I’m making a simple script reads out the nginx server status in node.js
I don’t think my problem has to do with node.js itself, but more an issue with how I’m using the setInterval() function.

I don’t want to paste all the code, because that makes a bit confusing to read.
When you run this code, you see this:

pre-Timer for web25
pre-Timer for web26
pre-Timer for web27
pre-Timer for web28
pre-Timer for web29
Timer for web29
Fetch host? web29
Timer for web29
Fetch host? web29
Timer for web29
Fetch host? web29
Timer for web29
Fetch host? web29
Timer for web29
Fetch host? web29

As you can see, the timer is only using the last host of the loop. Somehow it’s not making a copy of the variable to the setInterval scope.

What am I doing wrong?

Part of the code:

var http = require('http');

StatsNginxMapper.prototype = {

nginxServers: new Object(),
mysql: null,
mysqlClient: null,
statsDb: 'serverstats',
userMapper: null,

init: function() {
    this.mysql = require('mysql');
    this.mysqlClient = /* mysql stuff */;

    this.collectData();
},

setUserMapper: function(mapper) {
    this.userMapper = mapper;
},

collectData: function() {
    this.collectServers();
},

collectServers: function() {

    var self = this;
    var server = null;

    /* Normally this is done through MySQL, but for now lets do it manually */

    server = new StatsNginxServer();
    server.setHost('web25');
    this.nginxServers['web25'] = server;

    server = new StatsNginxServer();
    server.setHost('web26');
    this.nginxServers['web26'] = server;

    server = new StatsNginxServer();
    server.setHost('web27');
    this.nginxServers['web27'] = server;

    server = new StatsNginxServer();
    server.setHost('web28');
    this.nginxServers['web28'] = server;

    server = new StatsNginxServer();
    server.setHost('web29');
    this.nginxServers['web29'] = server;

    this.loopServers();

},

loopServers: function() {

    for(var host in this.nginxServers) {
        var nginxServer = this.nginxServers[host];

        if(nginxServer.hasTimer()) continue;

        var self = this;
        console.log('pre-Timer for ' + host);

        var timerId = setInterval(function() {

            console.log('Timer for ' + host);

            self.getData(host);

            }, 2000);

        nginxServer.setTimer(timerId);

    }

},

getData: function(host) {
    console.log('Fetch host? ' + host);
},

}

  • 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-01T22:24:19+00:00Added an answer on June 1, 2026 at 10:24 pm

    The problem is that the function that runs on an interval, executes with the variables that are in scope when it calls. What that means, is that the host that is ‘visible’ to every intervaled call, is the last one in the loop (in your case, web29).

    The solution is to make sure that the function that runs will always have the right one in scope. One way of doing this is:

    var timerId = setInterval(function(host) {
        return function() {
            console.log('Timer for ' + host);
            self.getData(host);
        }
    }(host), 2000);
    

    Here, we create a new function, that returns the function you wish to run. And we run that function immediately upon creation, passing through the value of ‘host’ at that time. This way, the function that does the logging, will always have the host that was in scope at the time of creation.

    I’ve tried to simplify the language to make it clearer, but I’m not sure I’ve succeeded. If you read through what I wrote, hopefully it should see clear.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters

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.