0

i try to develop a page and i have to use setTimeout function because i need to load my video in my page two seconds later.

In order to do that, i wrote this,

 window.onload = function () {
        player = new Player('playerObject');
        setTimeout(player.playByUrl($mp4Link),3000);
    }

but this is not working why ?

user1277467
  • 199
  • 1
  • 3
  • 12
  • Possible duplicate of [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Jun 14 '16 at 09:57

6 Answers6

3

Use callback:

setTimeout(function(){
   player.playByUrl($mp4Link);
},3000);

With your previous statement, the code was executing immediately (because you were calling it directly by specifying param and parenthesis eg playByUrl($mp4Link)) whereas setTimeout needs a callback.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 1
    It would be more helpful to say *why* and explain what the OP's original code is actually doing. – T.J. Crowder Mar 26 '12 at 12:56
  • @user1277467: Yes you are right. – Sarfraz Mar 26 '12 at 13:03
  • Player.prototype.playByUrl = function (url) { this.object.data = url; return this.play(); } this is my function that i call and this is not work window.onload = function () { player = new Player('playerObject'); setTimeout(player.playByUrl.bind(null,$mp4Link),3000); } why – user1277467 Mar 26 '12 at 13:04
3

You cannot add parameters to the function. However you can simply use an anonymuos function as callback and call the function in there (with the parameters).

window.onload = function () {
    player = new Player('playerObject');
    setTimeout(function() {
        player.playByUrl($mp4Link);
    }, 3000);
}
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
1

setTimeout requires a function

window.onload = function () {
   player = new Player('playerObject');
   setTimeout(function(){
       player.playByUrl($mp4Link);
   },3000);
}
slash197
  • 9,028
  • 6
  • 41
  • 70
1

You need to pass a function reference to setTimeout, but I guess the return value from .playByUrl() is not a function. So either go with

setTimeout(function() {
    player.playByUrl($mp4Link);
},3000);

or use ES5 .bind()

setTimeout(player.playByUrl.bind(null,$mp4Link),3000);
jAndy
  • 231,737
  • 57
  • 305
  • 359
0

player.playByUrl($mp4Link) is the returned value.

try

setTimeout("player.playByUrl($mp4Link)",3000);

or

setTimeout(player.playByUrl,3000, $mp4Link);

The latter does not work with IE and so should be modified.

onemach
  • 4,265
  • 6
  • 34
  • 52
  • window.onload = function () { player = new Player('playerObject'); setTimeout(player.playByUrl($mp4Link),10000); } this is not working why :( – user1277467 Mar 26 '12 at 13:21
  • I have explained above. `player.playByUrl($mp4Link)` is some value other than a function. – onemach Mar 26 '12 at 13:47
-1

try to add player.load(); as follow:

  player.load();
  setTimeout(function(){ 
    player.setAttribute("src","http://www.w3school.com.cn/example/html5/mov_bbb.mp4");
    player.play();
  },5000)
Dhia
  • 10,119
  • 11
  • 58
  • 69
xuanliwei
  • 1
  • 1