chieffancypants/angular-loading-bar — pull request #79 by chrisg2 (2014-07-11 07:49:17+00:00)
Title: Cancel any outstanding complete timeout before creating a new one

angular-loading-bar is a great simple tool to use! Thanks for your work.

I noticed that the bar often doesn't display when making a number of HTTP requests in quick succession where some requests are finishing in less than the latency threshold. The sequence of events is:

1. First HTTP request starts and finishes in less than the `latencyThreshold`, and `cfpLoadingBar.complete()` is called. `cfpLoadingBar.complete()` sets up the `completeTimeout` to clear the loading bar after 500ms.

1. Before the `completeTimeout` has occurred, a second HTTP request starts and finishes quicker than `latencyThreshold`. `cfpLoadingBar.complete()` is called again, and sets up a 2nd `completeTimeout` to clear the loading bar after 500ms.

1. Before either `completeTimeout` has occurred, a third HTTP request (this one running for longer) starts. `cfpLoadingBar.start()` is called after `latencyThreshold` and starts the loading bar. This cancels the 2nd `completeTimeout`, **but not the *first* `completeTimeout`**.

1. While the 3rd HTTP request is still running, the first `completeTimeout` fires and removes the loading bar.

This pull request addresses this by adding the following line to the `_complete()` function to ensure any existing `completeTimeout` gets cancelled before setting up another timeout:

```
$timeout.cancel(completeTimeout);
```

I have also added a unit test to verify the behavior; the test fails with this change and passes with the change.

I hope this is useful!