Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a timeout to the stop function as to avoid a stop and an instant request after that #43

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/loading-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,37 @@ angular.module('chieffancypants.loadingBar', [])
*/
var latencyThreshold = cfpLoadingBar.latencyThreshold;

/**
* The amount of time spent before actually stopping the bar
*/
var stopThreshold = cfpLoadingBar.stopThreshold;

/**
* $timeout handle for latencyThreshold
*/
var startTimeout;

/**
* $timeout handle for the stop timeout.
*/
var stopTimeout;


/**
* calls cfpLoadingBar.complete() which removes the
* loading bar from the DOM.
*/
function setComplete() {
$timeout.cancel(startTimeout);
cfpLoadingBar.complete();
reqsCompleted = 0;
reqsTotal = 0;

// Don't stop the bar immediately because if we get
// a request immediately after stopping it, it looks like
// it goes back, and the effect is not really good.
stopTimeout = $timeout(function () {
cfpLoadingBar.complete();
reqsCompleted = 0;
reqsTotal = 0;
}, stopThreshold);
}

/**
Expand Down Expand Up @@ -99,6 +115,10 @@ angular.module('chieffancypants.loadingBar', [])
// the requester didn't explicitly ask us to ignore this request:
if (!config.ignoreLoadingBar && !isCached(config)) {
$rootScope.$broadcast('cfpLoadingBar:loading', {url: config.url});

// Cancel the stop timeout because we just got another request.
$timeout.cancel(stopTimeout);

if (reqsTotal === 0) {
startTimeout = $timeout(function() {
cfpLoadingBar.start();
Expand Down Expand Up @@ -156,6 +176,7 @@ angular.module('chieffancypants.loadingBar', [])
this.includeSpinner = true;
this.includeBar = true;
this.latencyThreshold = 100;
this.stopThreshold = 0;
this.parentSelector = 'body';

this.$get = ['$document', '$timeout', '$animate', '$rootScope', function ($document, $timeout, $animate, $rootScope) {
Expand Down Expand Up @@ -282,6 +303,7 @@ angular.module('chieffancypants.loadingBar', [])
complete : _complete,
includeSpinner : this.includeSpinner,
latencyThreshold : this.latencyThreshold,
stopThreshold : this.stopThreshold,
parentSelector : this.parentSelector
};

Expand Down