Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Java C#3.5 script
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:
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 13-Oct-11 6:25am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Morgs Morgan at 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 at 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 at 14-Oct-11 1:31am
   
By the way i like your gadget tutoria :)
Morgs Morgan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Try this once for get results
 
<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
  Permalink  
Comments
Morgs Morgan at 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 at 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)

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 13 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100