Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

I have a situation where a user enters a search query and on each keyup event of the input box i set a timeout (setTimeout) to execute the search(asp.net script service call). But if the user is still typing a search query i have to clear the set time out.
I have tried something that looks reasonable but no no, not much joy!

Code:
JavaScript
var x = '';
$('#search').keyup(function ()
{
    clearTimeout(x);
    x = setTimeout("GetSearchResults($('#search'));", 100);
});


I tried increasing the timeout period to say 300, 400, 500 but then the query takes long to execute which is not preferable

Any advise of how i can resolve this or any other approach that i could use?
Will appreciate any help guys.

Happy coding,
Morgs
Posted

Seeing as you are already using JQuery, why not use the autocmplete plugin;
http://docs.jquery.com/Plugins/autocomplete[^]

could save you some t+e.
 
Share this answer
 
Comments
Morgs Morgan 13-Oct-11 16:18pm    
Hi, thanks for your suggestion. I had initially using considered using a custom tool for this task but because the requirements of my search will not work fully using the jQuery auto complete i had to create my own. The above code works ok as you can see i put clearTimeout() before setTimeout() and i assign an ID to a setTimeout. But here i would guess that if a user enters something in the input box it is supposed to clear the set timeout which it isn't at the moment. Anyway i will look for other ways of doing this...u can have 5 for a good link :)
DaveAuld 13-Oct-11 17:20pm    
Is your format of your setTimeout() correct? Look at the timing routine at the bottom of my article here: http://www.codeproject.com/KB/gadgets/CPRepWindowsGadget.aspx You will see i have setTimeout(function () {......}, xxx);
Morgs Morgan 14-Oct-11 1:31am    
By the way i like your gadget tutoria :)
Morgs Morgan 14-Oct-11 1:28am    
Hi Dave thanks for your feedback, yes even if i try using the setTimeout(function(){ GetSearchResults($('#search')); } ); it will still not work.
Hi,

Try this once for get results

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script language ="javascript" >
        function f1(tid) {
            $("#searchdiv").load("searchpage.aspx?action=get", {searchitem:tid.Value}, function (data) { });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
       <input type ="text" id="txtsearch" onkeyup="f1(this)" /> 
       <div id="searchdiv">
          
       </div>
    </div>
    </form>
</body>
</html>


here you've to write your search code in pageload of searchpage between querystring condition .

All the best
 
Share this answer
 
Comments
Morgs Morgan 13-Oct-11 16:11pm    
Hi, thanks for your response but i regret to say, i assume you didn't quite read the question fully. Your suggested solution will pretty much call for a full page load/request on every keypress/keyup event which to me appears not to be a good approach towards an auto complete operation. In my question, i stated that i would like to only perform a search once a user stops typing which is why i initially used setTimeout & clearTimeout algorithm. But thanks for your answer either way :)
Muralikrishna8811 14-Oct-11 4:43am    
Hi,
yeah not preferable.

But Autocomplete is only for pre data.If you want fetch data from server then you've to follow load or get method.

and one more thing is in load and get method no full page loading.
its just like invoking class on server.you can change page to handler.

I've another idea for your requirement.
when user press space then fetch data from server based on words.

If you want to search pre data thn just ignore this
All the Best

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900