Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ยิงฟังก์ชัน JavaScript เมื่อผู้ใช้พิมพ์เสร็จ ไม่ใช่ Key Up

เพื่อให้ฟังก์ชันทำงานเมื่อผู้ใช้พิมพ์เสร็จ (Debounce) ใช้โค้ดดังนี้

ตัวอย่าง

<html> 
  <body> 
    <script> 
      var timer = null; 
      $("#myText").keydown(function(){ 
        clearTimeout(timer); 
        timer = setTimeout(doStuff, 1000) 
      }); 
      function doStuff() { 
        alert("Write more!"); 
      } 
    </script> 
    <input type="text" id="myText" /> 
  </body> 
</html>