Wait for a certain period to execute a function in JavaScript?

1

Is there a method that makes a function run after a certain time? What I need is that the function below execute after a period of time, because the ID that it searches is only available after a time that we stayed on the page.

var func = document.getElementById('olvideo_html5_api').src;
alert(func);
window.location.href = +func
    
asked by anonymous 01.10.2017 / 03:57

1 answer

2

Use setTimeout() to execute a function after the specified time (in seconds):

tempo = 10; //especifique aqui os segundos
tempo = tempo*1000;
setTimeout(function(){
    var func = document.getElementById('olvideo_html5_api').src;
    alert(func);
    window.location.href = func;
}, tempo);

Or you can specify the time directly in the function:

setTimeout(function(){
    var func = document.getElementById('olvideo_html5_api').src;
    alert(func);
    window.location.href = func;
}, 10000); // o tempo é dado em milisegundos
    
01.10.2017 / 04:01