Tom Lehman
Tom Lehman

Reputation: 89193

How to setTimeout() with a jQuery function?

setTimeout(target.mousedown, 200) doesn't seem to work. I can do setTimeout("target.mousedown()", 200), but this feels dirty.

What's the right way to do this?

Upvotes: 0

Views: 473

Answers (3)

RaYell
RaYell

Reputation: 70414

You haven't given us too much of your code but this definitely works:

var target = {
    mousedown: function() {
        alert('foo');
    }
};

setTimeout(target.mousedown, 200);

Upvotes: 0

Warren Young
Warren Young

Reputation: 42333

You might like this better:

setTimeout(function() { target.mousedown(); }, 200);

Upvotes: 3

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827218

You can use an anonymous function:

setTimeout(function () {
  target.mousedown();
}, 200);

And you're right, you should always avoid using string parameters in setTimeout and setInterval functions.

Upvotes: 2

Related Questions