toxicious Posted May 27, 2012 Share Posted May 27, 2012 I'm trying to implement insta-search on my webpage, and I have got it working.But there is one problem: it doesn't seem to be completely async. Here is the page with the search box: http://gaddons.tk Oh, and don't visit it in IE since it doesn't work at all in IE (the whole div turns white lol, have to make another thread about that).If you type something quite fast the site will either hang or some of the letters you typed won't get typed, the syndroms of sync requests. I am searching "onkeyup" using this javascript method: function instaSearch(acount, searchTerm){//Abort opened requests to speed it upif(runningRequest){ request.abort();} //Add searchtip - this doesn't seem to get added either, though if I add alerts before and after I can see it....$('#searchBox').after('<p id="searchText">Searching...</p>');runningRequest=true;request = $.get('addonstable.php',{ search: encodeURIComponent(searchTerm) , count: acount},function(data){ $("#table").html(data); runningRequest=false;});//Remove searchtip$('#searchText').remove();} Any clue on how I can make it completely async?I know at least two pages that has a completely async search, Google and the Swedish site "prisjakt.nu", so it can't be a impossibility Link to comment Share on other sites More sharing options...
thescientist Posted May 27, 2012 Share Posted May 27, 2012 read the documentation. http://api.jquery.com/jQuery.get/ to make it async, you need to provide a callback function to be run when the request is completed, which is the function literal you are defining after you make the request. Link to comment Share on other sites More sharing options...
toxicious Posted May 27, 2012 Author Share Posted May 27, 2012 read the documentation. http://api.jquery.com/jQuery.get/ to make it async, you need to provide a callback function to be run when the request is completed, which is the function literal you are defining after you make the request. Yeah, realised now that I need to put the "remove code" for the searchtip there. But since I am providing that callback function, why does it still lag? Is there something else causing it maybe? Link to comment Share on other sites More sharing options...
thescientist Posted May 27, 2012 Share Posted May 27, 2012 (edited) show your updated code, but it didn't look like you were providing the callback correctly, which is what you need to make it aysnc. It needs to be a property called 'success', like in the docs Edited May 27, 2012 by thescientist Link to comment Share on other sites More sharing options...
toxicious Posted May 27, 2012 Author Share Posted May 27, 2012 function instaSearch(acount, searchTerm){//Abort opened requests to speed it upif(runningRequest){ request.abort();}else{ //Show searchtip $('#searchText').slideDown("200");}runningRequest=true;request = $.get('addonstable.php',{ search: encodeURIComponent(searchTerm) , count: acount},function(data){ $("#table").html(data); //Remove searchtip $('#searchText').stop(true); $('#searchText').slideUp("200"); runningRequest=false;});} As you can see, no big changes. I am using an anonymous function with no name. Are you sure it needs to be named success? Link to comment Share on other sites More sharing options...
thescientist Posted May 27, 2012 Share Posted May 27, 2012 (edited) did you read the documentation? (an example is right there at the top of the page with an explanation of 'success') You need to define a callback function in order for it to be asynchronous. That's how it works. And pass that in a property of the request object. You can name the function anything you want, or assign it the property anonymously. But the property needs to be called 'success' as dictated in the documentation. Edited May 27, 2012 by thescientist Link to comment Share on other sites More sharing options...
toxicious Posted May 29, 2012 Author Share Posted May 29, 2012 (edited) did you read the documentation? (an example is right there at the top of the page with an explanation of 'success') You need to define a callback function in order for it to be asynchronous. That's how it works. And pass that in a property of the request object. You can name the function anything you want, or assign it the property anonymously. But the property needs to be called 'success' as dictated in the documentation. Yes I read the docs, but I have to admit I skipped the jqXHR part since I had no interest in using that object. But saw the example now. Weird that it can succeed multiple times... Anyway, thanks a lot for clarifying for me! Solved some other problems I had too Edited May 29, 2012 by toxicious Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now