Skip to content

Commit

Permalink
cleaned up code, added funtions (will be documented) and fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SeverinDK committed Oct 22, 2016
1 parent 09c90d4 commit 5349c57
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 109 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simplefader",
"version": "1.0.0",
"version": "1.1.0",
"description": "Simple fader for fading between any amount of images with minimal markup generated.",
"main": "lib/simplefader.js",
"repository": {
Expand Down
10 changes: 5 additions & 5 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@
<div id="image-container"></div>
</div>
<script>
var fader = new Fader(document.getElementById("image-container"), [
var fader = new Fader("#image-container", [
"http://www.planwallpaper.com/static/images/6768666-1080p-wallpapers.jpg",
"http://wallpapercave.com/wp/vOJsqkP.jpg",
"http://www.planwallpaper.com/static/images/880665-road-wallpapers.jpg",
"https://wallpaperscraft.com/image/house_fairy_tale_art_light_night_101615_1920x1080.jpg",
"http://www.hdbloggers.net/wp-content/uploads/2016/08/Batman-1080p-HD-Wallpaper-Background.jpg",
]);

fader.setDisplayTime(5000);
fader.setFadeTime(2500);
fader.setDisplayTime(1000);
fader.setFadeTime(250);
fader.toggleRandomize();
fader.start();

new moment.duration(6000).timer(function() {
/*new moment.duration(6000).timer(function() {
fader.stop();
});
new moment.duration(10000).timer(function() {
fader.start();
});
});*/
</script>

<div id="content">
Expand Down
97 changes: 53 additions & 44 deletions example/vendor/moment-timer.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
// define internal moment reference
var moment;

if (typeof require === "function") {
try { moment = require('moment'); }
catch (e) {}
}

if (!moment && this.moment) {
moment = this.moment;
}

if (!moment) {
throw "Moment Timer cannot find Moment.js";
}

(function(root, undefined) {

function Timer(duration, loop, wait, start, callback) {
this.duration = duration;
this.timerDuration = duration;
this.callback = callback;
this.loop = loop;
this.paused = true;
this.started = false;
this.stopped = false; // If stop() is called this variable will be used to finish the paused duration once it's started again.
this.timer;
this.startTick;
Expand All @@ -39,7 +23,7 @@ if (!moment) {
}

Timer.prototype.start = function() {
if(this.paused) {
if(!this.started) {

var self = this;

Expand All @@ -50,25 +34,24 @@ if (!moment) {
return self.start();
}, this.getRemainingDuration());

this.endTick = Date.now() + this.getRemainingDuration();
this.stopped = false;
return true;
}

if(this.loop) {
this.timer = setInterval(function(){
self.updateTicks();
self.updateStartEndTickFromDuration(self.timerDuration);
return self.callback();
}, this.duration);
}, this.timerDuration);
} else {
this.timer = setTimeout(function(){
self.paused = true;
self.started = false;
return self.callback();
}, this.duration);
}, this.timerDuration);
}

this.updateTicks();
this.paused = false;
this.updateStartEndTickFromDuration(self.timerDuration);
this.started = true;

return true;
}
Expand All @@ -77,22 +60,49 @@ if (!moment) {
}

Timer.prototype.stop = function() {
if(!this.paused) {
if(this.started) {
this.clearTimer();
this.updateStartEndTickFromDuration(this.getRemainingDuration());
this.started = false;
this.stopped = true;
return true;
}

return false;
}

Timer.prototype.clearTimer = function() {
if(this.timer) {
if(this.loop) {
this.timer = clearInterval(this.timer);
} else {
this.timer = clearTimeout(this.timer);
}

this.updateTicksWithTicksRemaining();
this.paused = true;
this.stopped = true;
return true;
}

return false;
}

Timer.prototype.updateStartEndTickFromDuration = function(duration) {
this.startTick = Date.now();
this.endTick = this.startTick + duration;

return true;
}

Timer.prototype.duration = function() {
if(arguments.length > 0) {
this.timerDuration = moment.duration(arguments[0], arguments[1]).asMilliseconds();
this.stop();
this.start();
return true;
}

return false;
}

Timer.prototype.getRemainingDuration = function() {
if(this.startTick && this.endTick) {
if(this.stopped) {
Expand All @@ -105,25 +115,24 @@ if (!moment) {
return 0;
}

Timer.prototype.updateTicks = function() {
this.startTick = Date.now();
this.endTick = this.startTick + this.duration;

return true;
Timer.prototype.isStopped = function() {
return this.stopped;
}

Timer.prototype.updateTicksWithTicksRemaining = function() {
this.startTick = Date.now();
this.endTick = this.startTick + this.getRemainingDuration();
// define internal moment reference
var moment;

return true;
}
if (typeof require === "function") {
try { moment = require('moment'); }
catch (e) {}
}

Timer.prototype.clearTicks = function() {
this.startTick = null;
this.endTick = null;
if (!moment && this.moment) {
moment = this.moment;
}

return true;
if (!moment) {
throw "Moment Timer cannot find Moment.js";
}

moment.duration.fn.timer = function(attributes, callback) {
Expand Down
Loading

0 comments on commit 5349c57

Please # to comment.