需要Javascript延迟

时间:2011-08-10 19:25:40

标签: javascript

我想知道如何修改此代码,以便在函数变为活动状态之前有2秒的延迟:

<script type="text/javascript">
function iframe_onload()
{
var theWaitCell = document.getElementById('Wait1');
theWaitCell.style.display = "none";
}
</script>

任何帮助将不胜感激,

谢谢!

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

您可以使用setTimeout()功能:

<强>语法:

// 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);

<强>用法:

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

答案 2 :(得分:0)

使用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>

参考:https://developer.mozilla.org/en/DOM/window.setTimeout

另请参阅:https://developer.mozilla.org/en/DOM/window.clearTimeout

答案 3 :(得分:0)

我认为如果你想要一个很好的延迟你应该使用jquery但你也可以使用setTimeout()来调用输入中的函数;

<form>
<input type ="button lets say" onclick = "setTimeout('iframe_onload()', 2000);"/>
</form>