最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing variables from within object into setTimeout - Stack Overflow

programmeradmin0浏览0评论

I'm having some interesting issues with passing in variables from within an object into setTimeout. At first, I tried putting the function I was calling from setTimeout on my object so that I wouldn't have to pass any variables into it (I was hoping it could access my object by itself). That didn't work, apparently because the function somehow became global when I called it from setTimeout, and no longer had access to my object's variables.

This was my next attempt, but it doesn't work either:

function MyObj() {
    this.foo = 10;
    this.bar = 20;
    this.duration = 1000;

    setTimeout(function(){
        AnotherFunction(this.foo, this.bar)
    }, this.duration);
}

So, how exactly can I pass in a variable into setTimeout from within an object? No, AnotherFunction won't be able to directly access MyObj for various unrelated reasons, so that's out of the question too.

I'm having some interesting issues with passing in variables from within an object into setTimeout. At first, I tried putting the function I was calling from setTimeout on my object so that I wouldn't have to pass any variables into it (I was hoping it could access my object by itself). That didn't work, apparently because the function somehow became global when I called it from setTimeout, and no longer had access to my object's variables.

This was my next attempt, but it doesn't work either:

function MyObj() {
    this.foo = 10;
    this.bar = 20;
    this.duration = 1000;

    setTimeout(function(){
        AnotherFunction(this.foo, this.bar)
    }, this.duration);
}

So, how exactly can I pass in a variable into setTimeout from within an object? No, AnotherFunction won't be able to directly access MyObj for various unrelated reasons, so that's out of the question too.

Share Improve this question edited Mar 17, 2012 at 23:20 Elliot Bonneville asked Mar 17, 2012 at 23:11 Elliot BonnevilleElliot Bonneville 53.4k23 gold badges100 silver badges124 bronze badges 2
  • what do you hope to achieve here? what's AnotherFunction called for? by convention, constructors start with Capitalized letters. is AnotherFunction another constructor? or just a function to be called? – Joseph Commented Mar 17, 2012 at 23:21
  • It's very plex, that's why I didn't copy my actual code in here. Basically, 'for real', it's a function called ClearCharacter that clears an ASCII character off the page by calling another object's Draw() function, which overwrites MyObj's Draw() function, which is another function I didn't include here because it was irrelevant but that draws an ASCII character on the page via setting the innerHTML value of a <p> element. Like I said, plex and very irrelevant. So yeah, AnotherFunction is just a function to be called. =) – Elliot Bonneville Commented Mar 17, 2012 at 23:23
Add a ment  | 

1 Answer 1

Reset to default 7

I think the problem is that when your function executes, this is no longer bound to MyObj. You could try

function MyObj() {
    var that = this;
    this.foo = 10;
    this.foo = 20;
    this.duration = 1000;

    setTimeout(function(){AnotherFunction(that.foo, that.bar)}, this.duration);
}

Or I do have one more idea should that not work.

发布评论

评论列表(0)

  1. 暂无评论