0

I have text input like this and it will perform a search on every key input by user. Since the search function takes bit of time to execute user input is stuck until it finishes the execution.

 <input type="text" id="suggestion" onkeyup="search();"/>

Can we let this search function execute and let the user type in the text field. Something like letting the search run in background.

ps. I don't want to use a delaying function to wait until user finishes and call search()

asked Nov 5, 2015 at 22:18
4
  • 2
    Try Debouncing: davidwalsh.name/javascript-debounce-function Commented Nov 5, 2015 at 22:27
  • Where are you outputting your result? Commented Nov 5, 2015 at 22:45
  • I'm searching about 3000 rows to find some text details. And if there's anything matching to my search criteria a simple UI change will be done. That's the idea. Commented Nov 6, 2015 at 18:50
  • @Tim Ackroyd that answer is not what I'm looking for. That's what I meant by ps. guess. :) Btw thanks. Commented Nov 6, 2015 at 18:52

2 Answers 2

2

JavaScript is running on a single thread - therefore, when the search function is running, it also blocks the UI from rendering and responding to you. You should avoid as much as possible doing operations on the browser that last more than 16ms, you can read more about the reasons here.

Anyway, currently there are 2 primary solutions for this:

  • Move it to the server-side. What's the search logic that you run in JavaScript? should it really run on the client-side and not on the server-side?
  • Execute it on another thread. Are you familiar with WebWorkers? It's a more advanced topic, but generally it allow you to execute code on another thread. It's quite new and not fully supported yet in today's browsers, but you should at least be familiar with it.

Good luck.

answered Nov 5, 2015 at 22:41
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe instead of letting it run every time set a global switch using

window.somevar = true; 

Then check if that variable is set before searching again. Once the initial search is complete then show your results and unset the global variable

delete window.somevar;

That way the function "runs" each keystroke, but doesn't lock up the input.

answered Nov 5, 2015 at 22:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.