{"id":367,"date":"2010-02-25T10:22:01","date_gmt":"2010-02-25T04:52:01","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=367"},"modified":"2010-06-09T15:48:46","modified_gmt":"2010-06-09T10:18:46","slug":"implementing-delay-in-ajax-calls","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/implementing-delay-in-ajax-calls\/","title":{"rendered":"Implementing Delay Between Ajax Calls"},"content":{"rendered":"<p> Implementing search functionality on the keyup event using ajax has one pitfall. It can bombard our server with unnecessary ajax calls. So it becomes imperative for us to introduce some delay between the ajax calls. One way to implement it will be to make an ajax call after  some delay(say 500 ms) after the first character has been typed in the text box. But,a better solution will be  to make the ajax call after 500ms,  after the last character has been typed. The idea is to clear the timer on each key-up event till the user types in the last charecter.<br \/>\n This solution can be implemented as given below:<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"html\">\r\n<input type=\"text\" onkeyup=\"activateTimer(this.id);\" id=\"firstName\"\/>\r\n<\/pre>\n<pre lang=\"javascript\">\r\n var alertTimerId = 0;\r\nfunction activateTimer(objId) {\r\n    clearTimeout(alertTimerId);\r\n    alertTimerId = setTimeout('sendData(\"' + objId + '\")', 500);\r\n  }\r\n  function sendData(objId) {\r\n    ajax call \r\n  }\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Here, the instance or identifier of the time out call is captured in a variable and then this identifier is passed to clearTimeout function to clear the timer.<br \/>\nAnother problem associated with ajax search on keyup event is that if we are using tab button to navigate between different search fields, unnecessary ajax calls are shooted. To prevent this we can modify our activateTimer function like this :<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"javascript\">\r\nfunction activateTimer(objId, event) {\r\n    if (event.which == 9) {\r\n    }\r\n    else {\r\n      clearTimeout(alertTimerId);\r\n      alertTimerId = setTimeout('sendData(\"' + objId + '\")', 500);\r\n    }\r\n  }\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Event argument can be passed as parameter like this :<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"html\">\r\n  <input type=\"text\" onkeyup=\"activateTimer(this.id,event);\" id=\"firstName\"\/>\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Hope you find this useful.<br \/>\nImran Mir<br \/>\nimran@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Implementing search functionality on the keyup event using ajax has one pitfall. It can bombard our server with unnecessary ajax calls. So it becomes imperative for us to introduce some delay between the ajax calls. One way to implement it will be to make an ajax call after some delay(say 500 ms) after the first [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/367"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}