I have a similar feature in one of my windows applications. When the user enters a character, the timer starts at intervals of 1 second. In the Tick event, the search begins. If the user dials the number again, the timer restarts. Thus, the search is performed only if the keyboard is idle for more than a second.
A short sample (it's in C #, but simple enough to follow):
public partial class Form1 : Form
{
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
Console.WriteLine("Searching...");
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
}
timer.Start();
}
}
I am not familiar with Javascript, but the answer from here will help you, I think:
<input name="domain" type="text" id="domain" onKeyUp="javascript:chk_me();">
var timer;
function chk_me(){
clearTimeout(timer);
timer=setTimeout(function validate(){...},1000);
}
source
share