0plus1
0plus1

Reputation: 4555

Calling timeout inside an object, function get called immediately

I made a jsfiddle for you:

http://jsfiddle.net/fyJP2/

This is the code:

var chat = {

    init: function(){
        setTimeout( comet , 10);
    }
};

function comet()
{
    alert('called');
}

$(document).ready(function(){
  chat.init();
});

As you can see comet() is called immediately ignoring completely the delay of 10 seconds, why? I am passing comet and not comet() as setTimeout requires, but the function is still executed. I originally thought it was a scope issue so I moved comet to its own function, but doing this: http://jsfiddle.net/fyJP2/1/ the result is the same.

Can someone explain me why?

Upvotes: 2

Views: 66

Answers (1)

James Hibbard
James Hibbard

Reputation: 17735

As has been pointed out the second argument passed to window.setTimeout is in milliseconds. Therefore your code should be:

init: function(){
    setTimeout( comet , 10000);
}

Presuming you meant ten seconds, that is.

Read more here: https://developer.mozilla.org/en/docs/Web/API/window.setTimeout

Upvotes: 2

Related Questions