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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:53:27+00:00 2026-05-17T01:53:27+00:00

I want to keep everything contained within a single function, and not make all

  • 0

I want to keep everything contained within a single function, and not make all the various variables global.

Is it possible to set up an event listener within the function main to watch a global var?

in this example call doTheStuff() when the index is altered? or might I be going about this the wrong way?

var index = 0;


function main(data, data, data, data, data, data,)
{

function one(){ two() }

function two(){ three() }

function three(){ }

function doTheStuff(){ }

}
  • 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-17T01:53:28+00:00Added an answer on May 17, 2026 at 1:53 am

    There’s probably a better way to go about this, for instance:

    // An anonymous function to provide scoping
    (function() {
        var index = 0;  // A non-global, but accessible to everything within this scoping function
    
        function setIndex(newIndex) {
            index = newIndex;
            doTheStuff();
        }
    
        function one() { two(); }
    
        function two() { three(); }
    
        function three() {
            // Perhaps `three` needs to change the index
            setIndex(index + 1);
        }
    
        function doTheStuff() { }
    })();
    

    If that doesn’t work for what you’re trying to do, you can set up an interval timer and check the value against a saved copy.

    var index = 0;
    function main(...) {
        var lastIndex = index;
    
        setInterval(checkIndex, 100); // Every 100ms or so
    
        function checkIndex() {
    
            if (index != lastIndex) {
                lastIndex = index;
                doTheStuff();
            }
        }
    
        // Other functions omitted...
    
    }
    

    It would be possible for index to be changed more than once between intervals; whether that matters will depend on what you’re trying to do.

    Going forward, you’ll be able to do this with getters and setters on a property (part of the new ECMAScript 5 specification), but that’s still not widely-implemented. Some browsers do implement them, although with their own syntax, but others don’t (yet).

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

Sidebar

Related Questions

I want to keep everything within the parent div but the second div goes
I want keep all my source code and documentation in one single Git repository.
I'm developping a chat application. I want to keep everything logged into a table
I want to keep only numbers and remove all characters from a variable. For
I am trying to make some sort of timeline, but I can not keep
I want go full screen and keep everything inside in order. How should i
I want to keep my footer at the bottom of the page while keeping
i want to keep my app in sync with the Server. The communication between
I want to keep an element always visible, even when it should be scrolled
I want to keep my list GWT side after fetch it from service side.For

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.