Javascript 延迟运行功能3秒钟?

Javascript 延迟运行功能3秒钟?,javascript,jquery,sharepoint-2010,sharepoint-designer,Javascript,Jquery,Sharepoint 2010,Sharepoint Designer,我希望延迟运行此功能3秒: <script type="text/javascript"> $('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input') .on('focus', function(){ var $this = $(this); if($this.val() == '4/11/2013'){ $this.val('');

我希望延迟运行此功能3秒:

<script type="text/javascript">
$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
  .on('focus', function(){
      var $this = $(this);
      if($this.val() == '4/11/2013'){
          $this.val('');
      }
  })
  .on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          $this.val('4/11/2013');
      }
  });
</script>

$(“#FormsPageID表tr:n子(12)td:n子(2)div span input')
.on('focus',function(){
var$this=$(this);
如果($this.val()=“4/11/2013”){
$this.val(“”);
}
})
.on('blur',function(){
var$this=$(this);
如果($this.val()=''){
$this.val('4/11/2013');
}
});
我遇到的例子都涉及使用setTimeout或在X秒后显示元素。但我不确定这将如何应用于我的函数。

使用setTimeout:

setTimeout(function(){$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
 .on('focus', function(){
      var $this = $(this);
      if($this.val() == '4/11/2013'){
          $this.val('');
      }
  })
  .on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          $this.val('4/11/2013');
      }
  });}, 3000);
使用设置超时:

setTimeout(function(){$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
 .on('focus', function(){
      var $this = $(this);
      if($this.val() == '4/11/2013'){
          $this.val('');
      }
  })
  .on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          $this.val('4/11/2013');
      }
  });}, 3000);
您需要使用,因为它在另一方面触发一次,直到调用clear interval

$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
   .on('focus', function(){
    var $this = $(this);
    if($this.val() == '4/11/2013'){
       setTimeout(function(){ 
             $this.val('4/11/2013');
       }, 3000); 
     }
}).on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          setTimeout(function(){ 
             $this.val('4/11/2013');
          }, 3000); 
      }
});
设置超时时间

setTimeout()方法调用函数或计算表达式 在指定的毫秒数之后

setInterval

setInterval()方法调用函数或计算表达式 以指定的间隔(以毫秒为单位)

setInterval()方法将继续调用该函数,直到 调用clearInterval()或关闭窗口

您需要使用,因为它在另一方面触发一次,直到调用clear interval

$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
   .on('focus', function(){
    var $this = $(this);
    if($this.val() == '4/11/2013'){
       setTimeout(function(){ 
             $this.val('4/11/2013');
       }, 3000); 
     }
}).on('blur', function(){
      var $this = $(this);
      if($this.val() == ''){
          setTimeout(function(){ 
             $this.val('4/11/2013');
          }, 3000); 
      }
});
设置超时时间

setTimeout()方法调用函数或计算表达式 在指定的毫秒数之后

setInterval

setInterval()方法调用函数或计算表达式 以指定的间隔(以毫秒为单位)

setInterval()方法将继续调用该函数,直到 调用clearInterval()或关闭窗口

JQuery有自己的.delay()函数。 有关文件:

虽然我自己无法测试这个。这可能会给你一个小主意如何完成这一点

<script type="text/javascript">
$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input').delay(3000).focus(function () { 
    var $this = $(this);
    if($this.val() == '4/11/2013'){
        $this.val('');
    }
  }).delay(3000).blur(function() {
    var $this = $(this);
    if($this.val() == ''){
        $this.val('4/11/2013');
    }
  });
</script>

$('#FormsPageID表tr:nth child(12)td:nth child(2)div span input')。延迟(3000)。焦点(函数(){
var$this=$(this);
如果($this.val()=“4/11/2013”){
$this.val(“”);
}
}).延迟(3000).模糊(函数(){
var$this=$(this);
如果($this.val()=''){
$this.val('4/11/2013');
}
});
JQuery有自己的.delay()函数。 有关文件:

虽然我自己无法测试这个。这可能会给你一个小主意如何完成这一点

<script type="text/javascript">
$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input').delay(3000).focus(function () { 
    var $this = $(this);
    if($this.val() == '4/11/2013'){
        $this.val('');
    }
  }).delay(3000).blur(function() {
    var $this = $(this);
    if($this.val() == ''){
        $this.val('4/11/2013');
    }
  });
</script>

$('#FormsPageID表tr:nth child(12)td:nth child(2)div span input')。延迟(3000)。焦点(函数(){
var$this=$(this);
如果($this.val()=“4/11/2013”){
$this.val(“”);
}
}).延迟(3000).模糊(函数(){
var$this=$(this);
如果($this.val()=''){
$this.val('4/11/2013');
}
});

您想延迟什么?绑定事件的部分,还是要延迟事件主体?要延迟什么?您绑定事件的部分,还是要延迟事件主体?这对我不起作用。为了澄清这一点,我希望在页面加载3秒钟后运行此程序-一旦加载,onfocus和onblur就不需要任何延迟。将我所有的原始代码包装在setTimeout函数中会起作用吗?有输入错误,setTimeout应该是setTimeout,请使用live demo检查我的更新答案。如果我想使用当前日期而不是2013年11月4日,我将如何修改此日期以实现此目的?这对我不起作用。为了澄清这一点,我希望在页面加载3秒钟后运行此程序-一旦加载,onfocus和onblur就不需要任何延迟。将我所有的原始代码包装在setTimeout函数中可以吗?有输入错误,setTimeout应该是setTimeout,请使用live demo检查我的更新答案。如果我想使用当前日期作为值,而不是2013年11月4日,我将如何修改此值?谢谢,Gabitsh!工作得很好。如果我想使用当前日期而不是2013年11月4日,我将如何修改此日期以实现此目的?谢谢,Gabitsh!工作得很好。如果我想使用当前日期而不是2013年11月4日,我将如何修改此日期?如果我想使用当前日期而不是2013年11月4日,我将如何修改此日期以执行此操作?@manh2244抱歉我的反应太晚。在本主题中,您可以预览如何检索当前日期,只需构造日期并将其放入var中,您将使用该var而不是“4/11/2013”字符串。如果我想将当前日期用作值而不是4/11/2013,我将如何修改此值?@manh2244抱歉我的反应太晚。在本主题中,您可以预览如何检索当前日期,只需构造日期并将其放入将使用的变量,而不是“4/11/2013”字符串。