Prevent stale AJAX data in jQuery

I posted earlier about an ideal I had on preventing stale ajax data from being placed over fresh ajax data, well recently I implemented a simple method of doing this in Query.

Stale ajax data is data that you don’t want anymore. For instance lets say I have a list of cities, say 3000. I want a live filter on this list and I dislike having to press buttons (they are such a bother after all) so I write a script that will load the result filtered for whatever is in my text box every time I release a key in that box, and to keep in all simple I use the onkeyup command.

So I search for “Seattle”, a search is run for “S”, then “Se”, then “Sea” ect ect until I get “Seattle”. Each character is it’s typed. Now there are far more results for that contain “S” then there are that contain “Seattle” so naturally that search will take longer to process*. As such your page will return the “S” result before the “Seattle” result. So the script displays the “Seattle” results, then gets the “S” results and displays them instead, and in so making the search useless.

My initial ideal (even before posting the old post about this) would be to simple compare string sized, since a filter can only make the results smaller, not bigger. Although that in reality is not true, if you type the wrong character and delete it the results could then again be bigger. And the possible solution I posted before is actually a lot more work since it’s passing json variables back and comparing timestamps. This solution is the best I’ve though of, although it too has its downsides.

This solution simply cancels the previous ajax call before making the next one, the following jquery function is ran onkeyup or onkeydown.

Snipplr: http://snipplr.com/view/48097/prevent-stale-ajax-data-with-jquery/

The .abort() commands it’s what makes this work, killing off old ajax calls before making new ones. There are some caveats with this method. First, it doesn’t necessarily stop the server from processing the action (although according to the documentation it will try) so the server may keep doing it wasting cpu and ram on nothing. Second, the javascript error console (at least in chrome) sees it has an error when the .abort() happens. This doen’t cause any problems to the end user but can ugly up the console. I’d would guess though that the only people who would bother to look at the console are other programmers (because a non-programmer wouldn’t know exactly whats going on and it’s of no help to them) and they’ll be able to look at the code and see that it’s not truly an error. If you are uncomfortable with running the script with those problems theres always my previous ideal, it should be error free (but it’s all a theory I never put to use).

There’s no example for this one (although if requested I can make one).

* A simple single table search for names on even a large database would probably not have a problem, searching with a more complicated query across multiple databases and post processing those results both slow down the response and are far more common in my experience, especially since I usually don’t return json or XML but pre formatted ready to post HTML. This has the added advantage of (if properly set up) only requiring 1 script to call the data for both the initial load and any ajax loads, making future changes all the faster

Javascript Navigation Part Deux

Yesterday i posted some code related to using JavaScript to assis in keyboard navigation of a website, In that post I mentioned there was a problem when manually scrolling past chapter, today I’ve fixed that problem and am posting the solution.

The Solution is not particularly difficult, all you need to do is determine the users position and then determine which header that would go under. First things first, a interval must be set up to run a script every so often, when testing I use 3 seconds as it gave me enough to to scroll around the page and make sure it was all working well, but the final version of the script I moved up to every half second. Setting up a command to run over so often is easy.

I set that up in an init function that I called once the page was loaded (that function also included getting all the H1 tags and setting some variables) then comes the locator function

Snipplr: http://snipplr.com/view/45934/better-javascript-keyboard-navigation/

That code will process all of the h1 tags and find the one your actually reading, and set the currentpos variable (part of the larger script in the post Working with Javascript Input) to wherever you are allowing you to skip headers based on your current location instead of your last location.

This code is in another example (again, this one is modified to work better work with project gutenburg formatting, this time it’s using H5 tags) on the examples page.

This code was made with the GoogleTV in mind, so no testing was done outside of webkit, but it should work fine in firefox and IE (although it might need a few modifications). If anybody has an application in mind for web use let me know and I can help you tweak it to work in the big 3 desktop browsers.

There are two other improvements I have in mind that I might incorporate in the future. The first is being able to type in the chapter number and the second is implementing smooth scrolling instead of the current jump to method. I’m not sure if I’ll do those any time soon, but it all depends on the time I have.

Working with JavaScript key input

Recently I was selected to win a free googletv as part of their tv web developers program. Layout wise I don’t expect much to be different that any other device with a 1080p monitor, but the controller has some other media buttons that I though might have some use, and with a lack of a mouse, alternative control methods came into mind.

The devices media buttons are, unfortunately, outside of my testing ability because I can’t replicate their input, but assume the control unit has some basic arrow inputs* then it should be no problem to highjack those to do my bidding.

I’ve done a few things and decided to upload them to the Examples tab under javascript (once, you know, I program all the stuff and make it work, havent done that yet) so ou can check them out, all the code is in the file so it should be easy to look at and examine the construction process. The simplest (and probably the most convient to use) is below in all it’s coded glory. To make it short, it allows you to go between H1 tags with the Left and Right keyboard buttons.

Snipplr: http://snipplr.com/view/45909/javascript-keyboard-navigation/

There is a current problem with the system, if you manually scroll past a H1 tag then it doesn’t register it and press the next one may ump you back up the document. I’m pretty sure I have a solution but havent really had the time to implement it, and so It’s not in this code, I’ll update if/when I do fix that downside and pose the code.

You can view this code, along with some other modifications of the same concept on the example page. Also note, the example that uses this has been modified, it uses the H2 tag to work better with how project guttenburg lays out it’s HTML files.

*The device given to me is a Logitech Revue, which actually comes with a full keyboard control, but since I love my Logitech Harmony remote I’m much more likely to use that instead of whip out a keyboard. And I can see a time in the future where full keybaord layouts are probably either less common or, not particularly convient.

UPDATE: 8:58PM
the examples are up on the example page now, looks a little baren, just not enough time to fill it properly.