I want to trigger doSomething() only after a few seconds after an iframe is loaded but instead it triggers doSomething immediately why ?
<html>
<iframe id="myIframe" src="myFrame.html" width=100% height=600></iframe>
<script>
iframe = document.getElementById("myIframe");
function doSomething() {
alert("it should have wait 5000 milliseconds, instead it triggers immediately");
}
if (navigator.userAgent.indexOf("MSIE") > -1 && !window.opera) {
var oldonreadystatechange = iframe.onreadystatechange;
iframe.onreadystatechange = function(){
if (iframe.readyState == "plete"){
if (oldonreadystatechange != null) {
oldonreadystatechange();
setTimeout(doSomething(),5000);
}
}
};
} else {
var oldonload = iframe.onload;
iframe.onload = function(){
if (oldonload != null) {
oldonload();
}
setTimeout(doSomething(),5000);
};
}
</script>
</html>
I want to trigger doSomething() only after a few seconds after an iframe is loaded but instead it triggers doSomething immediately why ?
<html>
<iframe id="myIframe" src="myFrame.html" width=100% height=600></iframe>
<script>
iframe = document.getElementById("myIframe");
function doSomething() {
alert("it should have wait 5000 milliseconds, instead it triggers immediately");
}
if (navigator.userAgent.indexOf("MSIE") > -1 && !window.opera) {
var oldonreadystatechange = iframe.onreadystatechange;
iframe.onreadystatechange = function(){
if (iframe.readyState == "plete"){
if (oldonreadystatechange != null) {
oldonreadystatechange();
setTimeout(doSomething(),5000);
}
}
};
} else {
var oldonload = iframe.onload;
iframe.onload = function(){
if (oldonload != null) {
oldonload();
}
setTimeout(doSomething(),5000);
};
}
</script>
</html>
Share
Improve this question
asked Mar 13, 2016 at 13:36
user310291user310291
38.3k90 gold badges295 silver badges519 bronze badges
4
-
2
This as nothing to do with iframe but as how you call timeout callback... You want to use
setTimeout(doSomething,5000);
– A. Wolff Commented Mar 13, 2016 at 13:37 - @A.Wolff but then I cannot pass any parameter to doSomething ? – user310291 Commented Mar 13, 2016 at 13:39
-
You can pass extra param to setTimeout function or just wrap it inside anonymous function. Check the DOC, all is explained:
setTimeout(function(){doSomething(param1, param2)},5000);
orsetTimeout(doSomething,5000, param1, param2);
– A. Wolff Commented Mar 13, 2016 at 13:40 - @A.Wolff ok thanks I'm relearning javascript – user310291 Commented Mar 13, 2016 at 13:41
2 Answers
Reset to default 3If you want to also pass parameters, one very simple way is to create a new anonymous function for setTimeout like this:
setTimeout(function(){ doSomething(123, anotherParam) }, 5000);
That's because in JavaScript if you pass function to another function as a parameter like this someFunction(anotherFunction()); you are passing the result of the execution of that function to someFunction.
What you want to do is give the reference of that function to setTimeout so that setTimeout decides when to run it.
Do this:
setTimeout(doSomething, 5000);
Unless I'm totally missing something, if you are just firing on an iframe's load event you can cut down on that code by using this:
document.getElementById('myIframe').onload = function() {
setTimeout(doSomething, 5000);
}
Snippet
<iframe id="myIframe" src="/" width=100% height=600></iframe>
<script>
iframe = document.getElementById("myIframe");
function doSomething() {
alert("it should have wait 5000 milliseconds, instead it triggers immediately");
}
iframe.onload = function() {
setTimeout(doSomething, 5000);
}
</script>