En recherchant sur le net un compte à rebour, je me suis rendu compte qu'il n'y en a aucun qui gere les mois ! Ils s'arrêtes aux jours ! (exemple : 243 j , 14 h , 35 min , 19 sec)
J'ai donc écris une petite fonction qui permet de créer un compte à rebour (complet et exacte) très facilement.
/*
Creator : Christian Louis Gabriel METZLER (Neroth) neroth_AT_xeked.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Date.prototype.countDown = function()
{
if(!arguments[0] && typeof this.countDownFTC != "undefined")
arguments[0] = this.countDownFTC;
if(!arguments[0] && typeof this.countDownFTC == "undefined")
arguments[0] = function(){};
this.countDownFTC = arguments[0];
var actual = new Date();
var end = new Date();
end.setTime(this.getTime());
var msbe = (end-actual);
if(msbe < 0)
{
this.countDownFTC.call(this,0,0,0,0,0,0);
return 0;
}
var ms = msbe;
var sec = ms/1000;
ms = parseInt(ms%1000);
var mi = sec/60;
sec = parseInt((sec%60)+1);
var hou = mi/60;
mi = parseInt(mi%60);
var day = hou/24;
hou = parseInt((hou%24));
var d = 0;
var m = 0;
var y = 0;
var cur = new Date();
while(1)
{
if(d >= (32 - new Date(cur.getFullYear(),cur.getMonth(),32).getDate())+1)
{
m++;
d = 0;
}
if(m >= 12)
{
y++;
m = 0;
}
if(cur.getDate() == end.getDate() && cur.getMonth() == end.getMonth() && cur.getFullYear() == end.getFullYear())
{
break;
}
cur.setDate(cur.getDate()+1);
if(!(cur.getSeconds() >= end.getSeconds() && cur.getMinutes() >= end.getMinutes() && cur.getHours() >= end.getHours() && cur.getDate() == end.getDate() && cur.getMonth() == end.getMonth() && cur.getFullYear() == end.getFullYear()))
d++;
}
day = d;
var mon = m;
var yea = y;
this.countDownFTC.call(this,yea,mon,day,hou,mi,sec);
var that = this;
if(ms > 1000)
ms = 1000;
if(ms < 1)
ms = 1;
if(typeof this.countDownTimer == "undefined")
this.countDownTimer = {};
clearTimeout(this.countDownTimer);
this.countDownTimer = setTimeout(function(){that.countDown(that.countDownFTC);},ms);
};
Son utilisation :
var newYear = new Date((new Date().getFullYear()+1),0,1);
var f = function(year,month,day,hour,minute,second)
{
document.title = "Year : "+year+" , Month : "+month+" , Day : "+day+" , Hour : "+hour+" , Minute : "+minute+" , Second : "+second;
if(second == 0)
document.title = "Good year !!";
}
newYear.countDown(f);
J'attend vos commentaires et vos critiques ! (Il est possible que j'ai fais des erreurs de calculs , mais ça me semble exacte !)
Ce message a été modifié par Neroth - 28 mai 2011 - 20:19.



Haut











