如何在jQuery中使用“setTimeout”?

时间:2014-04-05 03:32:30

标签: javascript jquery html

我有以下代码:

<head>
  <script>
    $(document).ready(function() {
      window.setTimeout(function() {
        $("#Error").animate({margin:"-50px"});
        $("#errorCloseholder").animate({margin:"-50px"});
      }, 2000);
    });
  </script>
</head>

这个代码在jQuery的帮助下,应该会在2秒后隐藏一些元素。

不幸的是,此代码无效。它与jQuery有关吗?或者我有某种语法错误?

元素的id为Error和errorCloseholder,它们都是2秒后隐藏的错误消息。

提前致谢。

5 个答案:

答案 0 :(得分:2)

.ready()功能结束时出现语法错误。

      };); //this one remove the first semi colon
   </script>
</head>

An example

答案 1 :(得分:0)

$(document).ready(function () {
  setTimeout(function(){
    $("#Error").animate({margin: "-50px"});
    $("#errorCloseholder").animate({margin: "-50px"});
  }, 2000);
});

从您的脚本中删除&#39 ;;&#39;

答案 2 :(得分:0)

尝试在此处使用.load()事件......

$(document).ready(function() {
  $(window).load(function(){
    setTimeout(function(){
        $("#Error").animate({margin:"-50px"});
        $("#errorCloseholder").animate({margin:"-50px"});
    }, 2000);
   });
});

答案 3 :(得分:0)

看起来你有一个太多的分号。尝试删除n#34中的倒数第二个; .ready&#34;功能,看看是否有帮助。

答案 4 :(得分:0)

您可以使用setTimeout()方法在定义的no之后调用函数。毫秒。

$(document).ready(function () {   
  var x = document.getElementById("txt");

  setTimeout(function(){x.value="2 seconds"},2000);<br/>
  setTimeout(function(){x.value="4 seconds"},4000);<br/>
  setTimeout(function(){x.value="6 seconds"},6000);<br/>
}

此处,“txt”是标签或文本框。 结果将是这样的:
2秒
4秒
6秒

相关问题