Skip to content

Commit

Permalink
Fixed .abort() not cancelling every timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
staaky committed Jun 5, 2015
1 parent 90e0daa commit b0ee6e9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "voila",
"version": "1.2.0",
"version": "1.2.1",
"description": "A jQuery plugin that provides callbacks for images, letting you know when they've loaded.",
"keywords": [
"image",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "voila",
"title": "Voilà",
"version": "1.2.0",
"version": "1.2.1",
"description": "A jQuery plugin that provides callbacks for images, letting you know when they've loaded.",
"keywords": [
"image",
Expand Down
16 changes: 9 additions & 7 deletions src/imageready.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $.extend(ImageReady.prototype, {

// onload and a fallback for no naturalWidth support (IE6-7)
if (this.options.method == 'onload' || !this.supports.naturalWidth) {
setTimeout($.proxy(this.load, this));
this.load();
return;
}

Expand Down Expand Up @@ -146,15 +146,17 @@ $.extend(ImageReady.prototype, {
},

stopPolling: function() {
if (!this._polling) return;
clearTimeout(this._polling);
this._polling = null;
if (this._polling) {
clearTimeout(this._polling);
this._polling = null;
}
},

stopLoading: function() {
if (!this._onloadImage) return;
this._onloadImage.onload = function() { };
this._onloadImage.onerror = function() { };
if (this._onloadImage) {
this._onloadImage.onload = function() { };
this._onloadImage.onerror = function() { };
}
},

stopWaitingForRender: function() {
Expand Down
18 changes: 10 additions & 8 deletions voila.pkgd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Voilà - v1.2.0
* Voilà - v1.2.1
* (c) 2015 Nick Stakenburg
*
* http://voila.nickstakenburg.com
Expand Down Expand Up @@ -172,7 +172,7 @@ $.extend(ImageReady.prototype, {

// onload and a fallback for no naturalWidth support (IE6-7)
if (this.options.method == 'onload' || !this.supports.naturalWidth) {
setTimeout($.proxy(this.load, this));
this.load();
return;
}

Expand Down Expand Up @@ -291,15 +291,17 @@ $.extend(ImageReady.prototype, {
},

stopPolling: function() {
if (!this._polling) return;
clearTimeout(this._polling);
this._polling = null;
if (this._polling) {
clearTimeout(this._polling);
this._polling = null;
}
},

stopLoading: function() {
if (!this._onloadImage) return;
this._onloadImage.onload = function() { };
this._onloadImage.onerror = function() { };
if (this._onloadImage) {
this._onloadImage.onload = function() { };
this._onloadImage.onerror = function() { };
}
},

stopWaitingForRender: function() {
Expand Down
3 changes: 1 addition & 2 deletions voila.pkgd.min.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*!
* Voilà - v1.2.0
* Voilà - v1.2.1
* (c) 2015 Nick Stakenburg
*
* http://voila.nickstakenburg.com
*
* MIT License
*/
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):jQuery&&!window.Voila&&(window.Voila=a(jQuery))}(function(a){function b(c,d,e){if(!(this instanceof b))return new b(c,d,e);var f=a.type(arguments[1]),g="object"===f?arguments[1]:{},h="function"===f?arguments[1]:"function"===a.type(arguments[2])?arguments[2]:!1;return this.options=a.extend({method:"onload"},g),this.deferred=new jQuery.Deferred,h&&this.always(h),this._processed=0,this.images=[],this._add(c),this}a.extend(b.prototype,{_add:function(b){var d="string"==a.type(b)?a(b):b instanceof jQuery||b.length>0?b:[b];a.each(d,a.proxy(function(b,d){var e=a(),f=a(d);e=e.add(f.is("img")?f:f.find("img")),e.each(a.proxy(function(b,d){this.images.push(new c(d,a.proxy(function(a){this._progress(a)},this),a.proxy(function(a){this._progress(a)},this),this.options))},this))},this)),this.images.length<1&&setTimeout(a.proxy(function(){this._resolve()},this))},abort:function(){this._progress=this._notify=this._reject=this._resolve=function(){},a.each(this.images,function(a,b){b.abort()}),this.images=[]},_progress:function(a){this._processed++,a.isLoaded||(this._broken=!0),this._notify(a),this._processed==this.images.length&&this[this._broken?"_reject":"_resolve"]()},_notify:function(a){this.deferred.notify(this,a)},_reject:function(){this.deferred.reject(this)},_resolve:function(){this.deferred.resolve(this)},always:function(a){return this.deferred.always(a),this},done:function(a){return this.deferred.done(a),this},fail:function(a){return this.deferred.fail(a),this},progress:function(a){return this.deferred.progress(a),this}}),a.fn.voila=function(){return b.apply(b,[this].concat(Array.prototype.slice.call(arguments)))};var c=function(){return this.initialize.apply(this,Array.prototype.slice.call(arguments))};return a.extend(c.prototype,{supports:{naturalWidth:function(){return"naturalWidth"in new Image}()},initialize:function(b,c,d){return this.img=a(b)[0],this.successCallback=c,this.errorCallback=d,this.isLoaded=!1,this.options=a.extend({method:"onload",pollFallbackAfter:1e3},arguments[3]||{}),"onload"!=this.options.method&&this.supports.naturalWidth?(this.intervals=[[1e3,10],[2e3,50],[4e3,100],[2e4,500]],this._ipos=0,this._time=0,this._delay=this.intervals[this._ipos][1],void this.poll()):void setTimeout(a.proxy(this.load,this))},poll:function(){this._polling=setTimeout(a.proxy(function(){if(this.img.naturalWidth>0)return void this.success();if(this._time+=this._delay,this.options.pollFallbackAfter&&this._time>=this.options.pollFallbackAfter&&!this._usedPollFallback&&(this._usedPollFallback=!0,this.load()),this._time>this.intervals[this._ipos][0]){if(!this.intervals[this._ipos+1])return void this.error();this._ipos++,this._delay=this.intervals[this._ipos][1]}this.poll()},this),this._delay)},load:function(){var b=new Image;this._onloadImage=b,b.onload=a.proxy(function(){b.onload=function(){},this.supports.naturalWidth||(this.img.naturalWidth=b.width,this.img.naturalHeight=b.height),this.success()},this),b.onerror=a.proxy(this.error,this),b.src=this.img.src},success:function(){this._calledSuccess||(this._calledSuccess=!0,this.abort(),this._successRenderTimeout=setTimeout(a.proxy(function(){this.isLoaded=!0,this.successCallback(this)},this)))},error:function(){this._calledError||(this._calledError=!0,this.abort(),this._errorRenderTimeout=setTimeout(a.proxy(function(){this.errorCallback&&this.errorCallback(this)},this)))},abort:function(){this.stopLoading(),this.stopPolling(),this.stopWaitingForRender()},stopPolling:function(){this._polling&&(clearTimeout(this._polling),this._polling=null)},stopLoading:function(){this._onloadImage&&(this._onloadImage.onload=function(){},this._onloadImage.onerror=function(){})},stopWaitingForRender:function(){this._successRenderTimeout&&(clearTimeout(this._successRenderTimeout),this._successRenderTimeout=null),this._errorRenderTimeout&&(clearTimeout(this._errorRenderTimeout),this._errorRenderTimeout=null)}}),b});

0 comments on commit b0ee6e9

Please sign in to comment.