为了账号安全,请及时绑定邮箱和手机立即绑定

setTimeout方法里面的方法如果有参数怎么办

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>setTimeout</title>
    <script type="text/javascript">
      var delayTime;
      function showAlertAfter(waitingTime, message){
        delayTime = window.setTimeout('alert(message)', waitingTime);
      }

    </script>
  </head>
  <body>
    <input type="button" value="clickme" onclick="showAlertAfter(5000,'Hello world')"/>
  </body>
</html>

上面的代码,运行会没有反应,因为window.setTimeout('alert(message)', waitingTime);这一行不行,因为message是传进来的参数,但是要怎么改才能让它显示传进来的message呢

正在回答

2 回答

第 9 行,

        delayTime = window.setTimeout('alert(message)', waitingTime);

发现,setTimeout 里 alert 那的单引号去掉就可以执行了,但不清楚为什么不是 5s 后弹出提示框。


0 回复 有任何疑惑可以回复我~

定时器的方法里面不要乱传东西...谢谢

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

setTimeout方法里面的方法如果有参数怎么办

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号