如何将函数延迟到加载后的某个时间

how to delay a function from happening until a time after load

我想同时显示一个元素和隐藏一个元素,页面加载后 2 秒,我知道下面的代码不对,但我只是用它来帮助理解我的逻辑努力实现。

delay(2000).$('#customer_contact').hide().$('#customer_contact_edit_cont').show();

如何最好地编写此逻辑?

除了语法问题之外,delay() 函数旨在延迟计划在 jQuery 的 fx 队列中 运行 发生的动画。

如果你想延迟某个动作在动画之外发生,那么你可以使用 setTimeout(),像这样:

setTimeout(function() {
  $('#customer_contact').hide();
  $('#customer_contact_edit_cont').show();
}, 2000);

您应该使用 setTimeout 函数。

setTimeout(
   function(){ 
     //Do something 
   }, 5000);

此处5000是以毫秒为单位的时间。 (1 sec = 1000 ms)