A Seemingly Snappier User Interface
Say you've got a fancy user interface. You're leveraging AJAX well, you're making good use of best practices, and you've got fast servers that respond quickly. But your user actions still aren't quite as fast as you'd like. What else can you do?
If you've done things correctly, you've got a user interface that reacts positively to successful flows, but will also handle errors gracefully. Even though you're "doing it right," consider restructuring your application: assume success. Normally, your server is up and running and operations will be completely successfully. The edge case (hopefully) is the error case. Assuming this, we can create a slightly faster user interface.
Let's begin with a basic voting mechanism. You've got a control on your page that lets a user vote on an object, and a small bit of text to represent the state of said control. It may look something like this:
This example is meant to be easy and functional; hopefully your controls are prettier. Regardless, here's a common flow for this request and its callback states:
- Listen for the
clickevent on the control. - Send the request.
- Display a "loading" message or a typical spinner image.
- If the call comes back successfully, provide "success" feedback to the user and update the control.
- If the call throws an error, provide an error message.
This flow is extremely common in today's web. But we want something more immediately responsive, so let's restructure the flow:
-
Use
onmousedownbecause it will fire off just slightly sooner thanonclickwould. (This might not suit all user interfaces but in this case it's fine, and it wins us a few milliseconds.) - Provide "success" feedback to the user and update the control.
- Send the request.
- If the call throws an error, reset the controls and provide an error message.
This structure might seem a little off. Indeed, the error case even "lies" to the user for a few milliseconds if the request fails. But it covers all of its bases; none of the previous flow is omitted. Keep in mind that the error case is the edge case, and a very large majority of the time it's fine to assume success at first.
Using jQuery, here's what the code might look like:
And here's this flow working right here on the page: