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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:39:47+00:00 2026-05-12T22:39:47+00:00

this has interested me purely as research and personal development. i have a namespaced

  • 0

this has interested me purely as research and personal development. i have a namespaced set of functions / variables.

within 1 function I need to call another through setTimeout but keeping the scope to ‘this’. i am struggling with this a little, can’t seem to bind it for when the setTimeout runs.

var foo = {
    ads: ["foo","bar"],
    timeDelay: 3,
    loadAds: function() {
        var al = this.ads.length;
            if (!al)
                return; // no ads

            for(var i = 0; i < al; i++) {
                setTimeout(function() {
                    this.scrollAd(this.ads[i]);
                }.apply(this), this.timeDelay * 1000);
            }
        },
        scrollAd: function(adBlock) {
            console.log(adBlock);

        }
    };
};

the .apply(this) DOES change the scope as the console.log outputs the right object back, but it runs the function immediately and then the exception/warning comes up as the callback remains empty:

useless setTimeout call (missing quotes around argument?)

is there an elegant way of doing this at all? i know i could do

var _this = this;

and reference _this in the anon callback. for example, in mootools i’d use .bind(this) instead…

and no, as this involves animating, i don’t want to use " " around the string as it will need to be eval’d and would impact performance…

  • 1 1 Answer
  • 3 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-12T22:39:47+00:00Added an answer on May 12, 2026 at 10:39 pm
    for(var i = 0; i < al; i++) {
        setTimeout(function() {
            this.scrollAd(this.ads[i]);
        }.apply(this), this.timeDelay * 1000);
    }
    

    apply doesn’t bind a function, it calls it. So you execute the scroll straight away and then pass its return value (undefined) to setTimeout, which is ineffective.

    You probably meant to use a closure like this over this and the loop variable (which must be closed or it will be the same, post-loop value for every timeout):

    for(var i = 0; i < al; i++) {
        setTimeout(function(that, j) {
            return function() {
                that.scrollAd(that.ads[j]);
            };
        }(this, i), this.timeDelay * 1000);
    }
    

    However you may prefer to use the new ECMAScript Fifth Edition function binding feature, which has a much more compact syntax:

    for (var i= 0; i<al; i++)
        setTimeout(this.scrollAd.bind(this, this.ads[i]), this.timeDelay*1000);
    

    (There’s an implementation of function.bind for browsers that don’t have have it natively at the bottom of this answer.)

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

Sidebar

Related Questions

This has defeated me. I want to have a static class variable which is
This has been a rather problematic issue on numerous occasions. We have alot of
This has been stumping me for a bit. I have a Class written in
G'day, This has been asked before for VC++ but I am interested in the
I know this question has been discussed before, but i am interested in doing
this has baffled me for a couple of hours now. Why do i get
this has been an ongoing problem with me, ive been trying to make a
This has been a massive headache. We use Ning as a our platform for
This has been frustrating me for a while now. I started developing a site
This has probably something to do with my transformations, but right now I can't

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.