1

この関数の実行を 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>

私が遭遇した例はすべて、setTimeout を使用して X 秒後に要素を表示することを含みます。しかし、それが私の機能にどのように適用されるかはわかりません。

4

3 に答える 3

3

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);
于 2013-04-11T14:42:24.837 に答える
3

setTimeout()は一度起動されるため、使用する必要がありますが、 setInterval()は 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

setTimeout() メソッドは、指定されたミリ秒数の経過後に関数を呼び出すか式を評価します。

setInterval

setInterval() メソッドは、指定された間隔 (ミリ秒単位) で関数を呼び出すか、式を評価します。

setInterval() メソッドは、clearInterval() が呼び出されるか、ウィンドウが閉じられるまで関数を呼び出し続けます。

于 2013-04-11T14:42:55.823 に答える
0

JQuery には独自の .delay() 関数があります。ドキュメント: http://api.jquery.com/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>
于 2013-04-11T14:49:28.920 に答える