I have successfully been using AJAX for a long time to search a MySqli books database and recently discovered that the server is getting needlessly thrashed because the search activates on every keystroke. Apparently I should be using a JavaScript debounce routine which includes a delay after each key-stroke and only searches after keyboard has finished debouncing.
The Wiki tutorial was modified and I managed to return the results but only in the Debugger. I was not able to convert the AJAX Object to a HTML string and render the results to the screen.
Many, many, many thanks, .text() worked a treat and only one minor adjustment:
searchResultElem.innerHTML = result;
// resultDiv.innerHTML = result
You’ve no idea how long I have been fumbling in the dark and trying different functions JavaScript is quite involved and adding AJAX takes it way over my head
My script drastically requires eliminating all the junk and to make it more readable then… just need to get the relevant book covers, etc
The debouncing should be taking place on the client (as demonstrated in my code sample). Can I ask why you are passing it as a parameter to the server?
A while ago I was working on a similar transition, where you have both types of states, implement them both in the code, then update the code so that the default state doesn’t need to be specified, and then remove the now unwanted and unneeded state.
I’m hoping that he’s working on a similar transition
You mean a debounced version and a non-debounced version and that the client is grabbing that info from the query string before deciding which version to implement?
There are over 17,000 books updated from a XLS spreadsheet on an hourly basis and the search routine builds a query string to interrogate the MySQL database then formats and renders the search results.
The debouncing does occur in the browser and only a complete search string is passed instead of searching on every keystroke - causing unnecessary thrashing.
I am open to ideas on how to improve the current setup.
Edit:
I have left the mode parameters in place so SP users can still test and notice the difference. It just means loading a different JavaScript file. As mentioned the default input search is now debounced and not searching on every keystroke.
If it helps, I was recently working my way through the Second Edition of Refactoring and found that I was able to use some of what it said in the Refactoring API’s part, to achieve a multi-step transition in a very simple way.
A catalog of most of the refactorings is at https://refactoring.com/catalog/, but the book helps flesh out the examples with a more in-depth coverage.