Trouble with setTimeout with addEventListener

aRig picture aRig · Apr 20, 2015 · Viewed 12.1k times · Source

I have a vimeo video that I want to play 3 seconds after a button is clicked. I can get the video to play on click, but I can't seem to get the setTimeout in the right spot... any suggestions?

var iframe1 = document.getElementById("prelearn-1");
var player1 = $f(iframe1);

var prelearnBtn = document.getElementById("prelearn-1-btn");
prelearnBtn.addEventListener("click", setTimeout(function(){player1.api("play")}, 3000));

I'm using the vimeo froogaloop API.

Answer

SachinGutte picture SachinGutte · Apr 20, 2015

Just wrap it inside a function -

prelearnBtn.addEventListener("click", function(){
    setTimeout(function(){
        player1.api("play");
    }, 3000);
});