-1

player object has a method stopVideo()

1.False

setTimeout(player.stopVideo, 1000); //says something is undefined

2.True

setTimeout(stopVideo, 1000);

function stopVideo() {
    player.stopVideo();
}

What's the difference and why is this happening?

3
  • It appears in your first example 'stopVideo' is not defined while in your second example, it is (albeit I'm not sure it'll do anything) Commented Oct 18, 2013 at 6:12
  • give the code of the object definition of player. Commented Oct 18, 2013 at 9:18
  • Does this answer your question? Where is my 'this'? Using objects method as a callback function Commented Jun 22, 2024 at 5:20

5 Answers 5

3

The correct signature for the setTimeout function is as follows:

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);

Your second example works because you are actually defining a function within the setTimeout call.

Your first example is actually the second signature here. So for it to work, you'd have to deal with it as if it was code and pass it as a string (similar to eval() ).

setTimeout( "player.stopVideo()", 1000 );

Here is a link to the resource and an excerpt from the description of the parameters:

  • func is the function you want to execute after delay milliseconds.
  • code in the alternate syntax is a string of code you want to execute after delay milliseconds (using this syntax is not recommended for the same reasons as using eval())
Sign up to request clarification or add additional context in comments.

3 Comments

You can pass a function reference to setTimeout
I thought player.stopVideo would be interpreted as a function, no?
It should be (as the other answer shows). I'm not sure what is the case here.. With the minimal code example you have given, it should work.. JavaScript is a strange beast...
2

If I open the Chrome devtools console and paste in

var player = { stopVideo: function() { console.log('ohai'); }}
setTimeout(player.stopVideo, 1000);

Nothing undefined pops up. I think you need to provide a little more context and a better explanation of what is wrong. Is your stopVideo-function trying to access this?

Comments

0

Both are alias of each other. This should work:

setTimeout(player.stopVideo(), 1000);

Comments

0

Why 1. doesnt work. It is because player.stopVideo is not defined before setTimeout(....)

Why 2. works even player is not defined. when you declare a function stopVideo, the JS engerine will predefine this function at the begining. stopVideo function is defined before setTimeout(...). Thus it's OK.

3 Comments

If player is not defined in #2 - it will still show undefined.\
In #2, the stopVideo function is avaliable for setTimeout because stopVideo is predefined. It wont make any error.
player.stopVideo get called after 1 sec, player will be avaliable in most case. If you still worry about it you can check it before, for example: if (player) player.stopVideo()
0

Another reason why #1 doesnt work: when you pass player.stopVideo to setTimeout, the "this" object for the method is "window", not "player". So, probably, player.stopVideo wont works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.