Webmaster Hub: Fonction de compte à rebour (Année/Mois/Jour/Heure/Minute/Seconde) - Webmaster Hub

Aller au contenu

Page 1 sur 1
  • Vous ne pouvez pas commencer un sujet
  • Vous ne pouvez pas répondre à ce sujet

Fonction de compte à rebour (Année/Mois/Jour/Heure/Minute/Seconde) écrit en JavaScript Noter : -----

#1 L'utilisateur est hors-ligne   Neroth 

  • Groupe : Actif
  • Messages : 16
  • Inscrit(e) : 06-décembre 09
  • Genre:Homme
  • Localisation:Bas Rhin , Alsace
  • Société:XeKeD

Posté 28 mai 2011 - 03:12

Bonjour !

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.

0

#2 L'utilisateur est hors-ligne   Neroth 

  • Groupe : Actif
  • Messages : 16
  • Inscrit(e) : 06-décembre 09
  • Genre:Homme
  • Localisation:Bas Rhin , Alsace
  • Société:XeKeD

Posté 29 mai 2011 - 17:08

Petite modification ! Il utilisera beaucoup moins le cpu !

/*

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()
{
	var getyMd = function(end)
	{
	var cur = new Date();
	var d = 0;
	var m = 0;
	var y = 0;

		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++;
		}
	return {"year" : y,"month" : m,"day" : d};
	};

	if(!arguments[0] && typeof this.countDownFTC != "undefined")
	arguments[0] = this.countDownFTC;

	if(!arguments[0] && typeof this.countDownFTC == "undefined")
	arguments[0] = function(){};

	if(typeof this.currentDay == "undefined")
	this.currentDay = 0;

	if(typeof this.countDownValue == "undefined")
	this.countDownValue = {};

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 cDay = parseInt(day);

day = this.countDownValue["day"];

var mon = this.countDownValue["month"];

var yea = this.countDownValue["year"];

	if(cDay != this.currentDay)
	{
	var yMd = getyMd(end);

	day = yMd.day;

	mon = yMd.month;

	yea = yMd.year;
	}

this.currentDay = cDay;

this.countDownValue =
{
"year" : yea,
"month" : mon,
"day" : day,
"hour" : hou,
"min" : mi,
"sec" : sec
};

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);
};


0

#3 L'utilisateur est hors-ligne   Aenoa 

  • Groupe : Membre+
  • Messages : 282
  • Inscrit(e) : 28-octobre 10
  • Genre:Homme
  • Localisation:Tournai
  • Société:Aucune

Posté 30 mai 2011 - 15:28

Merci pour ce partage de script, il pourrait bien m'être utile :)
Par contre, en tant qu'utilisateur basique, je ne pense pas à regarder le titre de la page, surtout sur chrome, où tout est coupé. C'est donc difficilement lisible pour un simple visiteur.

Peut être proposer une fonction qui écrirais sur la page en elle même le countdown, et qui se remplacerais automatiquement chaque seconde ?
Mon (très petit) site personnel

"Ce n'est pas parce que l'on ne sait pas, que l'on ne peut pas. L'on peut tout apprendre, avec de la motivation."
0

#4 L'utilisateur est hors-ligne   Neroth 

  • Groupe : Actif
  • Messages : 16
  • Inscrit(e) : 06-décembre 09
  • Genre:Homme
  • Localisation:Bas Rhin , Alsace
  • Société:XeKeD

Posté 30 mai 2011 - 18:07

C'est juste un exemple le truc du titre ;) ! On peut faire plein de chose avec !
Test le script ^^ , il est different des autres ;).

Je met un How to use it plus parlent ^^ :

var maDate = new Date(2012,0,1); // On fait une nouvelle date (le jour de l'an 2012)
var maFunction = function() // La fonction qui est executé toutes les secondes avec comme arguments : 0 = année , 1 = mois , 2 = jour , 3 = heure , 4 = minute , 5 = seconde (donc pour avoir juste le nombres de mois restant : arguments[1])
{
document.getElementById("monCountDown").textContent = "Année : "+arguments[0]+", Mois : "+arguments[1]+", Jour : "+arguments[2]+", Heure : "+arguments[3]+", Minute : "+arguments[4]+", Seconde : "+arguments[5]; //Affiche le compte à rebour dans l'element avec l'id "monCountDown"
      if(arguments[5] == 0) // l'arguments[5] (donc seconde) est égal à 0 quand le compteur est terminé sinon il varit Toujours entre 1 et 60
      document.getElementById("monCountDown").textContent = "Bonne année !"; // Affiche "Bonne année !"
};
maDate.countDown(maFunction); // Je lance le countDown sur maDate avec comme argument maFunction
// Et voilà ! Le compte à rebour tourne :)



Mais tu peux aussi l'utiliser dans tes fonctions courantes ! :
var afficherUnePageBlancheDansDeuxMinutes = function()
{
var t = new Date();
t.setMinutes(t.getMinutes()+2);
      var f = function()
      {
            if(arguments[5] == 0)
            document.body.innerHTML = ""; // innerHTML c'est pour l'exemple ! Sinon utilise les fonctions du DOM !
      };
t.countDown(f);
};


0

Partager ce sujet :


Page 1 sur 1
  • Vous ne pouvez pas commencer un sujet
  • Vous ne pouvez pas répondre à ce sujet

1 utilisateur(s) en train de lire ce sujet
0 membre(s), 1 invité(s), 0 utilisateur(s) anonyme(s)