Что-то вроде это должно быть хорошим началом для вас:
$(function(){
$('#timeperiod1').mood({
range: [1, 7] // hours
});
$('#timeperiod2').mood({
range: [7, 12] // hours
});
$('#timeperiod3').mood({
range: [12, 24] // hours
});
});
// the jquery plugin
// TODO: add end of day re init
// add min/sec along with hours
$.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));
};
})();