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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:20:38+00:00 2026-05-13T15:20:38+00:00

In the code below I want to call the method OpenNextPage through a call

  • 0

In the code below I want to call the method OpenNextPage through a call back called by a function from a third part API. In the first time the method is being called by an instance of a class called Corridor. After called for the first time, the method is called again through the callBack passed to the LoadPage method until no more corridors are returned by the Api function getCorridors.

I’ve found some problems regarding the context of execution. The method OpenNextPage should always be executed in the same context it was in the first time (where mC is the object that holds the instance of this). But this is not happening, the OpenNextPage is being called in a different context. Any ideas?

Cheers

function startExec()
{
    var mC = new Corridor();
    mC.OpenNextPage();
}

Corridor.prototype.OpenNextPage = function()
{
    if(this.KeepLoading == false)
    {
        if(this.KeepLoadingTimer)
            clearTimeout(this.KeepLoadingTimer);        

        return 0;
    }

    this.ShowLoadingCorridorMsg();        
    this.LoadPage(++this.LoadedPages, this.OpenNextPage, 3 * this.ItemsPerPage);
}

//********************************************************************************
//Private
Corridor.prototype.LoadPage = function(page, callBack, pageSize)
{
    var ref = this;

    var mt = new MWsTraffic();
    var city = new MCity();

    city.name = cityList.getCurrentCity().name;
    city.state = cityList.getCurrentCity().state;

    var rr = new MResultRange();

    //This function is defined in a sort of public API available on the partner web site. 
    //So, as far as I know, the execution context will be changed and as of this point the 'this' will refer
    //to another object, that's why I have a this reference in the ref variable. This is what I think
    mt.getCorridors(city, rr,
        function(cInfo)
        {
            if (cInfo == null) 
            {   
                ref.KeepLoading = false;
                return null;
            }

            if( (cInfo.recordCount == 0) ) 
            {
                ref.KeepLoading = false;
                return null;
            }

            var e;
            for (var i = 0; i < cInfo.recordCount; i++) 
            {
                e = cInfo.corridor[i];                
                ref.totalCorridorsArray.push( new corridorContents(e.codCorridor, e.nameCorridor, e.levelCongested, e.point.x, e.point.y, false) );
            }

            if(callBack != null)
            {
                //Corridor.call(ref, callBack);
                callBack(); // <----  here's is the problem. This callback which points to the OpenNextPage function is being executed in another context. 
                            // I want to execute it in the same context OpenNextPage was called originaly (from inside startExec mC object)
            }
        }
    );    
}
  • 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-13T15:20:39+00:00Added an answer on May 13, 2026 at 3:20 pm

    You should use some kind of “bind” facility. This will help you

    function bind(context,fncname){
        return function(){
            context[fncname].apply(context,arguments);    
        }
    }
    
    var openNextPage = bind(this,"OpenNextPage");
    this.LoadPage(++this.LoadedPages, openNextPage, 3 * this.ItemsPerPage);
    

    Or instead of callBack do

    callBack.call(ref);
    

    But “bind” is better, cause sometimes you want to pass callbacks to some outside functions which have no pointer to “this”. So by binding them, you will always have a correct scope.

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

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer UIview has a "tag" property, so yes, you can tag… May 15, 2026 at 6:23 pm
  • Editorial Team
    Editorial Team added an answer Your selector will match an element: <my_frame id="my_cart"> … which… May 15, 2026 at 6:23 pm
  • Editorial Team
    Editorial Team added an answer use the callback function of your ajax... if you have… May 15, 2026 at 6:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.