javascriptsettimeoutaddeventlistenerfroogaloop

Trouble with setTimeout with addEventListener


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.


Solution

  • Just wrap it inside a function -

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