I was wondering how I could modify this code so that it has a 2 second delay before the function become active:
<script type="text/javascript">
function iframe_onload()
{
var theWaitCell = document.getElementById('Wait1');
theWaitCell.style.display = "none";
}
</script>
Any help would be appreciated,
Thanks!
asked Aug 10, 2011 at 19:25
user580523
4 Answers 4
function iframe_onload()
{
var timer = setTimeout(function() {
var theWaitCell = document.getElementById('Wait1');
theWaitCell.style.display = "none";
}, 2000);
}
answered Aug 10, 2011 at 19:27
Seth
6,2253 gold badges30 silver badges44 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can use the setTimeout() function:
Syntax:
// Fires yourFunction() after delayInMilliseconds has elapsed
// Note: You pass the function object as the first parameter
// do NOT execute the function here (i.e. omit the "()")
setTimeout(yourFunction, delayInMilliseconds);
Usage:
<script type='text/javascript'>
//Timeout Function (2000 ~ 2 Seconds)
setTimeout(iframe_onload, 2000);
//Action Function
function iframe_onload() {
var theWaitCell = document.getElementById('Wait1');
theWaitCell.style.display = "none";
}
</script>
Ivo Wetzel
46.8k17 gold badges102 silver badges112 bronze badges
answered Aug 10, 2011 at 19:28
Rion Williams
76.9k38 gold badges207 silver badges335 bronze badges
2 Comments
Beez
Wouldn't "yourFunction()" and "iframe_onload()" execute immediately? I think you want "setTimeout(iframe_onload, 2000);"
Rion Williams
That is correct - I believe the edit has been made :) Thanks to Beez and Ivo for the edit.
Use setTimeout.
<script type="text/javascript">
setTimeout(
function ()
{
var theWaitCell = document.getElementById('Wait1');
theWaitCell.style.display = "none";
},
2000 // The 2nd arg is delay in milliseconds
);
</script>
Reference: https://developer.mozilla.org/en/DOM/window.setTimeout
See also: https://developer.mozilla.org/en/DOM/window.clearTimeout
answered Aug 10, 2011 at 19:27
FishBasketGordo
23.2k4 gold badges60 silver badges92 bronze badges
Comments
I think if you want a nice delay you should use jquery but you could also call the function in the input with setTimeout();
<form>
<input type ="button lets say" onclick = "setTimeout('iframe_onload()', 2000);"/>
</form>
answered Aug 10, 2011 at 19:37
Miek
1,1374 gold badges20 silver badges37 bronze badges
Comments
lang-js