0
$(document).ready(function(){
    var keyUpTime = 0;                  
    var t = 0;
    var executeAfterOneSecond = false;
    $('#source').keyup(function(){
        if(executeAfterOneSecond == false){
            executeAfterOneSecond = true;
            t = setTimeout(function(){
                executeAfterOneSecond = false;
                sendValue($('#source').val());                          
                }, 600);
            }                                   
        keyUpTime = $.now();
        setTimeout(function(){
            if($.now() - keyUpTime >= 200) {
                clearTimeout(t);
                executeAfterOneSecond = false;
                sendValue($('#source').val());          
                }           
            },200);
    });
});

<textarea id="source" name="text" wrap="soft" tabindex="0" dir="ltr" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" style="box-sizing: border-box; overflow-y: hidden; overflow-x: auto; padding-right: 20px; " class="oggy-textarea">
                                </textarea>

さて、私のコードが change(); で動作することを望みます。[$('#source').change(function(){---});]、keyup() ではありませんが、そうではありません。bind() で試しましたが、何もしませんでした。どうしたの?

4

1 に答える 1

1

Change イベントは、inputコントロールが失われた場合にのみ発生しfocusます。そのため、なぜ起動しないのですか。

ここの注を参照してください: http://www.w3schools.com/jquery/event_change.asp

keyUpkeyPressまたはを使用する必要がありますkeyDown

メソッドも本当にクリーンアップできます。次のようなものは、より安定している可能性があります。

これはsendValue()、最後のキーアップから 1 秒後に呼び出されます。22行を6行に減らします:)

jsFiddleの場合の例として、以下のコードを次に示します。

   $(document).ready(function(){

      $("#source").keyup(function(){
         clearTimeout(this.timer);
         this.timer = setTimeout(function() {sendValue($("#source").val());}, 1000);
      });

   });
于 2012-11-22T18:32:36.213 に答える