Current location: Hot Scripts Forums » General Web Coding » JavaScript » Delaying ajax call on input (jQuery)


Delaying ajax call on input (jQuery)

Reply
  #1 (permalink)  
Old 01-12-09, 05:13 PM
ste ste is offline
Newbie Coder
 
Join Date: May 2006
Location: Wales, UK
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Delaying ajax call on input (jQuery)

Heya guys,

I'm in the process of building a registration form and I'm checking for the validity of the username and then also checking to see if it exists in the db.

At the moment it's firing on every keyup (because of the code). I was just wondering if anyone could recommend howto pause this request until the user has finished typing.

Here's current state of the code:

javascript Code:
  1. $("span#username input").keyup(function () {
  2.             var username = $("span#username input").val().length;
  3.             var username_input = $("span#username input").val();
  4.             if(username >= 2)
  5.             {
  6.                 valid_username = 1;
  7.                 check_username_exists(username_input);    // function that makes the Ajax request            
  8.                 $("span#username img").attr("src","images/tick.png").attr("alt","Valid Username Information");
  9.             }
  10.             else if(username == "")
  11.             {
  12.                 $("span#username img").attr("src","images/asterisk_orange.png");
  13.                 valid_username = 0;
  14.             }
  15.             else
  16.             {
  17.                 $("span#username img").attr("src","images/exclamation.png");
  18.                 valid_username = 0;
  19.             }
  20.            
  21.             check_valid_inputs();      
  22.         });

Thank you for your time!
Steve
Reply With Quote
  #2 (permalink)  
Old 01-13-09, 12:29 AM
daveganley daveganley is offline
Newbie Coder
 
Join Date: Jun 2003
Location: London UK
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
What about when the input loses focus

Dave
Reply With Quote
  #3 (permalink)  
Old 01-13-09, 03:21 AM
drowzee drowzee is offline
Newbie Coder
 
Join Date: Dec 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
ste,

use javascript setTimeout(code, timeout_milliseconds) function, something like that:
Code:
var _timeout = null;
$("span#username input").keyup(function() { if(_timeout != null) { clearTimeout(_timeout); _timeout = null; } _timeout = setTimeout('_checkUsername()', 2000); } );

function _checkUsername()
{
  _timeout = null;
  // your code here
}
this sample will wait for 2 seconds after user stop typing and then will send check request.
Reply With Quote
  #4 (permalink)  
Old 01-13-09, 04:21 AM
ste ste is offline
Newbie Coder
 
Join Date: May 2006
Location: Wales, UK
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Morning guys,

Thanks for the input so far

Drowzee, I have opted to go with the code you suggested, but for some reason when it comes to the timeout and firing the function it says it's not defined.

Here's the updated version of my code:

javascript Code:
  1. $("span#username input").keyup(function () {
  2.             var username = $("span#username input").val().length;
  3.             var username_input = $("span#username input").val();
  4.             if(username >= 2)
  5.             {
  6.                 valid_username = 1
  7.                 $("span#username img").attr("src","images/tick.png").attr("alt","Valid Username Information");
  8.                 var _timeout = null;
  9.                 if(_timeout != null)
  10.                 {
  11.                     clearTimeout(_timeout);
  12.                     _timeout = null;
  13.                 }
  14.  
  15.                 _timeout = setTimeout('check_username_exists(username_input)', 2000);
  16.             }
  17.             else if(username == "")
  18.             {
  19.                 $("span#username img").attr("src","images/asterisk_orange.png");
  20.                 valid_username = 0;
  21.             }
  22.             else
  23.             {
  24.                 $("span#username img").attr("src","images/exclamation.png");
  25.                 valid_username = 0;
  26.             }
  27.            
  28.             check_valid_inputs();      
  29.         });  // end keyup
  30.  
  31.         function check_username_exists(username){
  32.             alert(username);
  33.             _timeout = null;
  34.             $("span#username img").after("<img src='images/ajax-loader.gif' alt='' />");
  35.             $.get("act_checkusername.php", { username: username }, function(found)
  36.             {
  37.                 if(found == 1)
  38.                     $("span#username img").after("<p>Username taken</p>");
  39.                 else
  40.                     $("span#username img").after("<p>Username available</p>");
  41.             });
  42.         }

I think I'm missing pretty simple, but I can't quite see it!
Reply With Quote
  #5 (permalink)  
Old 01-13-09, 04:35 AM
drowzee drowzee is offline
Newbie Coder
 
Join Date: Dec 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Hm, does it say that 'username_input' variable is not defined, or it says that 'check_username_exists' function is not defined?
Try rewriting statement like that:

_timeout = setTimeout('check_username_exists("'+escape(userna me_input)+'")', 2000);
Reply With Quote
  #6 (permalink)  
Old 01-13-09, 04:51 AM
ste ste is offline
Newbie Coder
 
Join Date: May 2006
Location: Wales, UK
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the quick response drowzee, the original error said:

"check_username_exists is not defined
_timeout = setTimeout('check_username_exists(username_input)' , 2000); "

and using your updated recommendation it produced:

"check_username_exists is not defined
_timeout = setTimeout('check_usernam...s("'+escape(username_i nput)+'")', 2000);"
Reply With Quote
  #7 (permalink)  
Old 01-13-09, 05:00 AM
drowzee drowzee is offline
Newbie Coder
 
Join Date: Dec 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Ok. I'm not sure of javascript framework you're using but this looks like syntax error in check_username_exists function (4-th line):
Code:
$.get("act_checkusername.php", { username: username }, function(found)
 ^ shouldn't there be some parenthesis with arguments?
Reply With Quote
  #8 (permalink)  
Old 01-13-09, 07:43 AM
ste ste is offline
Newbie Coder
 
Join Date: May 2006
Location: Wales, UK
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
javascript Code:
  1. _timeout = setTimeout(function() {check_username_exists(username_input);}, 3000)

Seems to make it work, however, when i monitor the GET requests it is still doing a reqeust for every character, doh!
Reply With Quote
  #9 (permalink)  
Old 01-13-09, 08:57 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
that's pretty logical: as you're setting a timeout every time the user hits a key.

When a user hits a key, the previous timeout should be unset, and a new one needs to be created.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #10 (permalink)  
Old 01-13-09, 09:16 AM
ste ste is offline
Newbie Coder
 
Join Date: May 2006
Location: Wales, UK
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah, your right UnrealEd, I've overlooked this.

Dave suggested I code it to detect when it has lost focus (which would be $().blur()) but ideally I'm trying to do it while the user is still within the input, but also keep the number of get requests down.

Could you possibly recommend another way I should approach this, while the user is still on the same input?

Thanks for the your continued help guys
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Jquery Ajax ... hanging up sometimes? phpdoctor JavaScript 8 12-19-08 11:19 PM
Ajax and jQUERY cancer10 JavaScript 1 06-25-08 12:56 AM
a.c. 50Hz voltage asm trouble steph6 Other Languages 0 04-09-08 05:17 AM
Asembly Language Help needed..Urgent..plzzzz paritoshcool Other Languages 1 11-27-07 07:07 AM
TreeView Control - Background Image? tim8w Windows .NET Programming 1 04-11-07 12:43 PM


All times are GMT -5. The time now is 11:45 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.