Javascript 设置超时don';我不能正常工作

Javascript 设置超时don';我不能正常工作,javascript,Javascript,我想在我点击按钮后,带有OK字符串的弹出窗口将在5秒后显示,但在我点击按钮后弹出窗口立即显示,为什么 谢谢 <html> <head> <title>Wake up call</title> <script type="text/javascript"> function wakeUpCall() { // Function is defined here setTimeout(a

我想在我点击按钮后,带有OK字符串的弹出窗口将在5秒后显示,但在我点击按钮后弹出窗口立即显示,为什么

谢谢

<html>
<head>
    <title>Wake up call</title>
    <script type="text/javascript">
        function wakeUpCall() { // Function is defined here
            setTimeout(aa("ok"), 5000);
        }

        function aa(bb) {
            alert(bb);
        }
    </script>
</head>
<body bgcolor="lightblue">
    <form>
        <input type="button"
            value="Wake me"
            onclick="wakeUpCall()">
    </form>
</body>
</html>

叫醒电话
函数wakeUpCall(){//函数在此处定义
设置超时(aa(“正常”),5000);
}
功能aa(bb){
警报(bb);
}

如果您想这样做怎么办:

<html>
<head>
    <title>Wake up call</title>
    <script type="text/javascript">
        function wakeUpCall() { // Function is defined here
            setTimeout('aa("ok");', 5000);
        }

        function aa(bb) {
            alert(bb);
        }
    </script>
</head>
<body bgcolor="lightblue">
    <form>
        <input type="button"
            value="Wake me"
            onclick="wakeUpCall()">
    </form>
</body>
</html>
function wakeUpCall() { // Function is defined here
    setTimeout(aa, 5000, "Your tekst here");
}

如果你这样做会怎么样:

<html>
<head>
    <title>Wake up call</title>
    <script type="text/javascript">
        function wakeUpCall() { // Function is defined here
            setTimeout('aa("ok");', 5000);
        }

        function aa(bb) {
            alert(bb);
        }
    </script>
</head>
<body bgcolor="lightblue">
    <form>
        <input type="button"
            value="Wake me"
            onclick="wakeUpCall()">
    </form>
</body>
</html>
function wakeUpCall() { // Function is defined here
    setTimeout(aa, 5000, "Your tekst here");
}
为此使用匿名函数。

叫醒电话
函数wakeUpCall(){//函数在此处定义
setTimeout(函数(){aa(“ok”);},5000);
}
功能aa(bb){
警报(bb);
}
为此使用匿名函数。

叫醒电话
函数wakeUpCall(){//函数在此处定义
setTimeout(函数(){aa(“ok”);},5000);
}
功能aa(bb){
警报(bb);
}

您试图以错误的方式进行操作

您必须对
设置超时使用回调:

setTimeout(function()
{
    // actual code here
}, 5000);
Mike在他的回答中提供了-您可以使用可评估字符串:

setTimeout('/* actual code here */', 5000);
但这是非常不鼓励的,使用他的另一个例子——将回调函数作为引用传递并调用回调参数。 不过,您必须记住,如果使用回调参数,请参阅本文。并非所有浏览器都支持回调参数

就个人而言,我建议使用普通的旧回调,因为setTimeout就是这样使用的

仅供参考:

您的代码段不适合您的原因是:

setTimeout(aa('ok'), 5000);
// aa('ok') here is executed, and returns its value, so, in the end, you pass the returned value of aa inside the Timeout.
// and, nor alert alert, nor your function have a "return" statement, so they both will  return always undefined.
// that translates to:

setTimeout(undefined, 5000); // and, that does nothing

你正试图用错误的方式去做

您必须对
设置超时使用回调:

setTimeout(function()
{
    // actual code here
}, 5000);
Mike在他的回答中提供了-您可以使用可评估字符串:

setTimeout('/* actual code here */', 5000);
但这是非常不鼓励的,使用他的另一个例子——将回调函数作为引用传递并调用回调参数。 不过,您必须记住,如果使用回调参数,请参阅本文。并非所有浏览器都支持回调参数

就个人而言,我建议使用普通的旧回调,因为setTimeout就是这样使用的

仅供参考:

您的代码段不适合您的原因是:

setTimeout(aa('ok'), 5000);
// aa('ok') here is executed, and returns its value, so, in the end, you pass the returned value of aa inside the Timeout.
// and, nor alert alert, nor your function have a "return" statement, so they both will  return always undefined.
// that translates to:

setTimeout(undefined, 5000); // and, that does nothing