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.

Just wrap it inside a function -

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

addEventListener + setTimeout, I'm asking because the function addEventListener() and setTimer don't I've added this in case anybody else has any setTimeout issues. setTimeout: Calls a function once after a specified delay. setInterval: Calls a function continuously at a specified delay between each call. setTimeout. The setTimeout method expects 2 arguments: a reference to a callback function and a delay in milliseconds. The following will print a message to the console after 1 second:

The setTimeout function takes in a callback function. Just wrap your code in an anonymous function, then it should work. Also, you might have cross-domain issues if you're attempting to access the contents of an iFrame.

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

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

Using js event listener and setTimeout in an extension on Project , More details here https://stackoverflow.com/questions/60553643/ava-test- settimeout-on-addeventlistener. catalinct opened this issue on Mar 5 � 1 comment. With setTimeout(), there’s a relatively long delay while the expression is evaluated, the function called, and the new setTimeout() being set up. So if you need regular, precise timing – or you need to do something at, well, set intervals – setInterval() is your best bet. clearInterval()

AddEventlistener takes anonymous function as the second argument. So you need to wrap the setTimeout inside the anonymous function. Whenever the click event happens, the setTimeout code will trigger and will play the video.

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

Anyone knows how to test with setTimeout() and addEventListener , Examples. Log a message when the page is fully loaded: window. addEventListener('load', (event) => { console.log('page is fully loaded'); });. Use setTimeout to allow the page to be rendered before your code runs. Deep Dive. The question of when your JavaScript should run comes down to ‘what do you need to interact with on the page?’. Scripts have access to all of the elements on the page which are defined in the HTML file before the script tag.

ES2015 shorter syntax + utilizing the 3rd argument of the setTimeout will make our code shorter and nicer while keeping it readiable:

document.querySelector('button').addEventListener("click",() => 
  setTimeout(console.log, 1000, "play")
)
<button>Play</button>

Window: load event, debounce(callback, wait) { let timeout; return (args) => { const context = this; clearTimeout(timeout); timeout addEventListener('scroll', () => { // Check how far the user has scrolled });. The problem here is the quantity of times it's fired. An imam began the formal prayer service at 1.45 p.m., after Erdogan read out a Koranic recitation and the call to prayer rang out from the mosque’s minarets

Understanding how a JavaScript ES6 debounce function works , As a result, the anonymous function being passed to setTimeout() is being defined in the Memory leaks are almost inevitable JavaScript problems if you're not addEventListener('click', function (event) { console.log("This is element #" + � The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress.

The 10 Most Common Mistakes JavaScript Developers Make, There are two methods for it: setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly� Here is an unofficial solution, we have to use the setTimeout() fucntion to delay the focus() execute time. setTimeout(function() { document.getElementById('myInput').focus(); }, 10); After delay the execute time, the focus() is working fine in IE , or even FF.

Scheduling: setTimeout and setInterval, Chrome has since fixed this issue, behaving more like Firefox. Older debounce techniques relied on setTimeout() . addEventListener('scroll', function (event) { console.log('no debounce'); // If there's a timer, cancel it if� pic.addEventListener(“load”, function {ctx.drawImage (pic,0,0) }, false); Rather than using setTimeout you should probably be using To be honest I had trouble understanding the

Comments
  • What do you mean by "I can't seem to get the setTimeout in the right spot'?