2017-11-22 22 views
0

私は私のkeypress fuctionがすべてのプレスで動作しますが、setTimeoutは動作しません。ここに私のHTMLとスクリプトです。一度働く方法setTimeout機能はキー押し機能で

HTML:

<div id="fake" class="full-height col-md-3 col-sm-12 col-xs-12"> 
     <div class="nav-right"> 
      <div class="form-search"> 
       <input id="fake-search" onkeypress="dialogTrigger(event)" class="fake-search" type="text" /> 
      </div> 
     </div> 
    </div> 

スクリプト:

function dialogTrigger(event) { 
    var keyboardInput = event.which; 
    keyboardInput = keyboardInput + 5; 
    var changedInput = String.fromCharCode(keyboardInput); 
    $("#fake-search").val($("#fake-search").val() + changedInput); 
    setTimeout(function() { 
     doSomething(); // about html 
    }, 10000); 
} 
+0

onkeyupイベントで書き込みを試してください – nivendha

+0

onkeyupでsetTimeout関数は一度しか動作しませんでした。私はthnxを試みた。 – Alperb

+0

var timeout = setTimeoutを使用する必要があります。次に、キーが押されるたびにタイムアウトをキャンセルして別のタイムアウトを開始するか、現在のものを放置して、どの場合に上書きしないかを決める必要があります。今、あなたはあなたのダイアログを複数回発射します。 – MartinWebb

答えて

1

あなたはロジックが何であるかを決定する必要があります。アイデアは、10秒後に一度開いているダイアログを絞るのであれば、あなたは彼らが

var to; 
    function dialogTrigger(event) { 
     var keyboardInput = event.which; 
     keyboardInput = keyboardInput + 5; 
     var changedInput = String.fromCharCode(keyboardInput); 
     $("#fake-search").val($("#fake-search").val() + changedInput); 

     // The line below clear any existing timeout and start a new one 

     if (to) clearTimeout(to); 
     to = setTimeout(function() { 
      doSomething(); // about html 
     }, 10000); 
    } 
を入力し終えた後にのみ、ダイアログを開きたい場合は、あなたは一方で、この

var to; 
function dialogTrigger(event) { 
    var keyboardInput = event.which; 
    keyboardInput = keyboardInput + 5; 
    var changedInput = String.fromCharCode(keyboardInput); 
    $("#fake-search").val($("#fake-search").val() + changedInput); 

    // The line below will only start the timer if none exists; 

    to = to || setTimeout(function() { 
     doSomething(); // about html 
    }, 10000); 
} 

ような何かをする必要があります

メモ10000msは約10秒です

+0

Thnxたくさん!私はちょうど10秒後に一度開いたダイアログにしたい。最初は正しく動作しています。 – Alperb

関連する問題