Skip to content

Instantly share code, notes, and snippets.

@p5150j
Created April 25, 2012 21:57
Show Gist options
  • Select an option

  • Save p5150j/2493841 to your computer and use it in GitHub Desktop.

Select an option

Save p5150j/2493841 to your computer and use it in GitHub Desktop.
time o day
$.fn.mood = (function(){
var Mood = function(el, opts){
this.el = $(el);
this.range = { bottom: opts.range[0]*1, top: opts.range[1]*1 };
this.init();
};
Mood.prototype = {
init: function(){
this.initTime = this.checkTime(); // 1, 0, -1
this.initTime == 0 ? this.show() : this.hide();
this.start();
},
start: function(){
var t = new Date(),
showDate = new Date(t),
hideDate = new Date(t),
h = t.getHours(), hide = false, show = false;
if(this.initTime < 0) {// time has not yet come
showDate.setHours(this.range.bottom);
showDate.setMinutes(0);
show = true;
}
if(this.initTime <= 0){
hideDate.setHours(this.range.top);
hideDate.setMinutes(0);
hide = true;
}
debugger;
show && setTimeout($.proxy(this.show, this), Math.ceil(showDate.getTime()-t.getTime()));
hide && setTimeout($.proxy(this.hide, this), Math.ceil(hideDate.getTime()-t.getTime()));
},
checkTime: function(){
var t = new Date(), h = t.getHours();
if(h >= this.range.bottom && h <= this.range.top)
return 0;
if(h < this.range.bottom)
return -1;
if(h > this.range.top)
return 1;
},
show: function(){
this.el.show('slow');
},
hide: function(){
this.el.hide('slow');
}
};
return function(opts){
return this.data('rotateMood', new Mood(this, opts));
};
})();
$(function(){
$('#timeperiod1').mood({
range: [1, 7]
});
$('#timeperiod2').mood({
range: [7, 15]
});
$('#timeperiod3').mood({
range: [15, 24]
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment