Owzzz
Owzzz

Reputation: 177

Method inside Object undefined?

I'm still learning how to use Javascript properly and have got stuck trying to set an interval on a function which is inside an object.

Here is my code:

http://pastebin.com/hJAtaqKi

On line 36 I am calling the function which is further down the page.. this works fine and references the function correctly.. but when I try to wrap that function in a setinterval setInterval('homePageFunctionality.animateCarousel($pause)', 4000);

It does not seem to work.. I get a homePageFunctionality is not defined..

Any idea why I cannot seem to reference the function when using setInterval?

Any other general feedback on my code is welcome.

Thanks

Upvotes: 1

Views: 242

Answers (2)

Gareth
Gareth

Reputation: 138012

Don't pass strings to setInterval or setTimeout. If you do, the parser has to eval them to turn them in to code, and it does that inside its own execution context so your variables aren't available.

Instead pass an anonymous function which does the same thing to setInterval:

setInterval(function() {
    homePageFunctionality.animateCarousel($pause);
}, 4000);

Upvotes: 3

Headshota
Headshota

Reputation: 21449

setInterval(function(){
 homePageFunctionality.animateCarousel($pause);
}, 4000);

try this.

Upvotes: 0

Related Questions