Question: I have a method that uses the [`setTimeout`](https://developer.mozilla.org/en/window.setTimeout) function and makes a call to another method. On initial load method 2 works fine. However, after the timeout, I get an error that says `method2` is undefined. What am I doing wrong here?

ex:

```
test.prototype.method = function()
{
    //method2 returns image based on the id passed
    this.method2('useSomeElement').src = "http://www.some.url";
    timeDelay = window.setTimeout(this.method, 5000);
};

test.prototype.method2 = function(name) {
    for (var i = 0; i < document.images.length; i++) {
        if (document.images[i].id.indexOf(name) > 1) {
            return document.images[i];
        }
    }
};

```

Answer: 
A more elegant option is to append `.bind(this)` to the end of your function. E.g.:

```
    setTimeout(function() {
        this.foo();
    }.bind(this), 1000);
//   ^^^^^^^^^^^ <- fix context

```

So the answer to the OP's question could be:

```
    test.prototype.method = function()
    {
        //method2 returns image based on the id passed
        this.method2('useSomeElement').src = "http://www.some.url";
        timeDelay = window.setTimeout(this.method.bind(this), 5000);
        //                                       ^^^^^^^^^^^ <- fix context
    }; 

```
The `this` you used in `setTimeout` is scoping via itself. Create a `var _this = this;` inside your `test.prototype.method` function and use `_this` instead.