0

I am trying to give an object a countdown timer and when the countdown is down it should call a function which removes this object from an array.

array = [];

var object = {
    'name' : 'user',
    'limit' : function() {
         setTimeout(destroyMe(this),10000);
    }
}

array.push(object);

var destroyMe = function(obj) {
    array.remove(obj);
}

I know that there could a problem with the this but the timeout function doesnt work at all not even like this:

var object = {
    'name' : 'user',
    'limit' : function() {
               setTimeout(console.log("dd"),3000);
            }
}

Maybe someone can tell me the issue with my version of the setTimeout. thx

1
  • [].remove? Never heard of it. And you need to do a array[0].limit() to initiate the countdown. Commented Dec 30, 2012 at 23:20

3 Answers 3

7

setTimeout takes a reference to a function. The code you have is calling the function.

This should be changed to:

var object = 
{
    'name' : 'user',
    'limit' : function() 
    {
        setTimeout(function() { destroyMe(this); }, 10000);
    }
}

(You may have issues using this in this context, try it!)

var object = 
{
    'name' : 'user',
    'limit' : function() 
    {
        setTimeout( function() { console.log("dd"); },3000);
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

this will be the global object. He needs to use some other variable pointing to the correct object.
first thing's first, call array[0].limit(); somewhere.
I was hoping that the Timout function would call itself after 3 seconds! But it doesnt if i use the code above!
@user1354743: Just put the setTimeout call after the object.
@user1354743 - No, that won't work. You're calling the function, not passing a reference to a function.
|
1

You need to pass a function to setTimeout, not a function call.

var object = {
    'name' : 'user',
    'limit' : function() {
         setTimeout(function() { 
             destroyMe(this)
         }, 10000);
    }
};

As you already know, the this probably doesn't do what you are expecting. Replacing destroyMe(this) with console.log("dd") should result in a behavior that you would expect.

Comments

1

setTimeout either takes a callback (a function, not a function call with arguments) or code in quotes. However, as noted in this question, passing a string is only allowed for historical reasons--there aren't any good reasons for doing it in practice.

But, for completeness sake, one way to do what you want is:

var object = {
    'name' : 'user',
    'limit' : function() {
               setTimeout('console.log("dd")', 3000);
            }
}

2 Comments

Please do not suggest passing a string. Ever. There is just not a single acceptable reason to do pass a string instead of a function. Besides that he needs this to be preserved.
If mentioning the string passing, you must IMO also warn that it's a pretty terrible way to do things.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.