开发者

In need of a Javascript Delay

I was wondering h开发者_运维百科ow 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!


function iframe_onload()
{
    var timer = setTimeout(function() {
        var theWaitCell = document.getElementById('Wait1');
        theWaitCell.style.display = "none";
    }, 2000);
}


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>


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜