par
naholyr » 03 mars 2006, 00:45
Comme je suis dans ma période Javascript, et puisque tu ne sembles pas disposé à vouloir faire beaucoup d'efforts de recherche, voici un objet bien propre pour faire un chrono. Je te laisse comprendre par toi-même comment ça fonctionne.
La classe "Chrono" :
Code : Tout sélectionner
var ChronoInstances = new Array(); // need a global pointer for setInterval
function Chrono (startValue, stopValue, step, intervalms, layerID) {
// -- Public attributes --
this.startValue = null;
this.stopValue = null;
this.step = null;
this.currentValue = null;
this.layerID = null
this.interval = null;
this.running = null;
// -- Constructor and private attributes --
this._layer = new Array();
this._intervalHandler = null;
this.startValue = startValue!=null ? startValue : 0;
this.step = step ? step : 1;
this.stopValue = stopValue!=null ? stopValue : this.startValue-this.step;
this.interval = intervalms ? intervalms : 1000; // milliseconds
this.running = false;
this.layerID = layerID;
// generate random ID to have a public function pointer to this.tick
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
while (true) {
for (var i=0; i<5; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
if (!ChronoInstances[randomstring]) {
this._randomID = randomstring;
ChronoInstances[this._randomID] = this;
break;
}
}
}
Chrono.prototype.start = function () {
if (!this.running) {
this.running = true;
this.currentValue = this.startValue;
this.onStart();
this.unpause();
}
}
Chrono.prototype.restart = Chrono.prototype.start; // alias
Chrono.prototype.unpause = function () { // start without reset value, "onStart" event not triggered
this.running = true;
this._intervalHandler = setInterval("ChronoInstances[\""+this._randomID+"\"].tick();", this.interval);
this.tick();
}
Chrono.prototype.tick = function () {
if (this.running) {
if ( (this.step>0 && this.stopValue>this.startValue && this.currentValue>=this.stopValue) || (this.step<0 && this.stopValue<this.startValue && this.currentValue<=this.stopValue) ) {
this.stop();
}
else {
this.onTick();
this.writeToLayer(this.toString());
this.currentValue += this.step;
}
}
}
Chrono.prototype.stop = function () {
this.pause();
this.onStop();
}
Chrono.prototype.pause = function () { // stop, "onStop" event not triggered
this.running = false;
clearInterval(this._intervalHandler);
}
Chrono.prototype.writeToLayer = function (str) {
if (this.layerID) {
if (!this._layer[this.layerID])
this._layer[this.layerID] = document.getElementById(this.layerID);
this._layer[this.layerID].innerHTML = str;
}
}
// -- override to customize
Chrono.prototype.onTick = function () { }
Chrono.prototype.onStop = function () { }
Chrono.prototype.onStart = function () { }
Chrono.prototype.toString = function () { return this.currentValue; }
Deux exemples d'utilisation :
Code : Tout sélectionner
<html>
<head>
<style type="text/css">
#chrono1, #chrono2 {
display: block;
width: 300px;
height: 25px;
border: 1px solid #ccc;
text-decoration: none;
font-size: 20px;
text-align: center;
}
#log {
display: block;
width: 300px;
height: 200px;
border: 1px solid #ccc;
overflow: auto;
}
</style>
<script src="chrono.js"></script>
<!-- Mon chrono personnalisй -->
<script>
function doLog(str) {
var d = document.getElementById("log");
d.innerHTML = str+"<br>"+d.innerHTML;
}
var c1 = new Chrono(0, 2000, 1, 100, "chrono1");
c1.toString = function() {
var v = this.currentValue;
v = Math.floor(v/10);
var s = v%60;
v = Math.floor(v/60);
var m = v%60;
v = Math.floor(v/60);
var h = v%24;
v = Math.floor(v/24);
var j = v;
var str = (j>0?j+'j':'')+(h>0?h+'h':'')+(m>0?m+'m':'')+(s>0?s+'s':'');
return str ? str : "0s";
}
c1.onTick = function () { doLog("tick! current value = "+this.currentValue); }
c1.onStart = function () { doLog("start : from "+this.startValue+" to "+this.stopValue+" by "+this.step); }
c1.onStop = function () { doLog("<b>stop! final value = "+this.currentValue+"</b>"); }
</script>
<!-- Mon second chrono -->
<script>
var c2 = new Chrono(10, 0, -1, 1000, "chrono2");
c2.onStop = function () { this.writeToLayer("BOUM !"); }
</script>
</head>
<body>
<fieldset>
<legend><strong>Chrono 1 (loggй) : 0 а 2000, interval = 1/10s</strong></legend>
<p>Clic = pause/continue. Double-clic = stop/start.</p>
<a href="#" ondblclick="if (c1.running) c1.stop(); else c1.start();" onclick="if (c1.running) c1.pause(); else c1.unpause();" id="chrono1">Lancer/arrкter le chrono</a>
<div id="log"></div>
</fieldset>
<fieldset>
<legend><strong>Chrono 2 (comptes а rebours) : 10 а 0, interval = 1s</strong></legend>
<a href="#" onclick="c2.start()" id="chrono2">Lancer le compte а rebours</a>
</body>
</html>
Comme je suis dans ma période Javascript, et puisque tu ne sembles pas disposé à vouloir faire beaucoup d'efforts de recherche, voici un objet bien propre pour faire un chrono. Je te laisse comprendre par toi-même comment ça fonctionne.
La classe "Chrono" :
[code]var ChronoInstances = new Array(); // need a global pointer for setInterval
function Chrono (startValue, stopValue, step, intervalms, layerID) {
// -- Public attributes --
this.startValue = null;
this.stopValue = null;
this.step = null;
this.currentValue = null;
this.layerID = null
this.interval = null;
this.running = null;
// -- Constructor and private attributes --
this._layer = new Array();
this._intervalHandler = null;
this.startValue = startValue!=null ? startValue : 0;
this.step = step ? step : 1;
this.stopValue = stopValue!=null ? stopValue : this.startValue-this.step;
this.interval = intervalms ? intervalms : 1000; // milliseconds
this.running = false;
this.layerID = layerID;
// generate random ID to have a public function pointer to this.tick
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
while (true) {
for (var i=0; i<5; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
if (!ChronoInstances[randomstring]) {
this._randomID = randomstring;
ChronoInstances[this._randomID] = this;
break;
}
}
}
Chrono.prototype.start = function () {
if (!this.running) {
this.running = true;
this.currentValue = this.startValue;
this.onStart();
this.unpause();
}
}
Chrono.prototype.restart = Chrono.prototype.start; // alias
Chrono.prototype.unpause = function () { // start without reset value, "onStart" event not triggered
this.running = true;
this._intervalHandler = setInterval("ChronoInstances[\""+this._randomID+"\"].tick();", this.interval);
this.tick();
}
Chrono.prototype.tick = function () {
if (this.running) {
if ( (this.step>0 && this.stopValue>this.startValue && this.currentValue>=this.stopValue) || (this.step<0 && this.stopValue<this.startValue && this.currentValue<=this.stopValue) ) {
this.stop();
}
else {
this.onTick();
this.writeToLayer(this.toString());
this.currentValue += this.step;
}
}
}
Chrono.prototype.stop = function () {
this.pause();
this.onStop();
}
Chrono.prototype.pause = function () { // stop, "onStop" event not triggered
this.running = false;
clearInterval(this._intervalHandler);
}
Chrono.prototype.writeToLayer = function (str) {
if (this.layerID) {
if (!this._layer[this.layerID])
this._layer[this.layerID] = document.getElementById(this.layerID);
this._layer[this.layerID].innerHTML = str;
}
}
// -- override to customize
Chrono.prototype.onTick = function () { }
Chrono.prototype.onStop = function () { }
Chrono.prototype.onStart = function () { }
Chrono.prototype.toString = function () { return this.currentValue; }
[/code]
Deux exemples d'utilisation :
[code]<html>
<head>
<style type="text/css">
#chrono1, #chrono2 {
display: block;
width: 300px;
height: 25px;
border: 1px solid #ccc;
text-decoration: none;
font-size: 20px;
text-align: center;
}
#log {
display: block;
width: 300px;
height: 200px;
border: 1px solid #ccc;
overflow: auto;
}
</style>
<script src="chrono.js"></script>
<!-- Mon chrono personnalisй -->
<script>
function doLog(str) {
var d = document.getElementById("log");
d.innerHTML = str+"<br>"+d.innerHTML;
}
var c1 = new Chrono(0, 2000, 1, 100, "chrono1");
c1.toString = function() {
var v = this.currentValue;
v = Math.floor(v/10);
var s = v%60;
v = Math.floor(v/60);
var m = v%60;
v = Math.floor(v/60);
var h = v%24;
v = Math.floor(v/24);
var j = v;
var str = (j>0?j+'j':'')+(h>0?h+'h':'')+(m>0?m+'m':'')+(s>0?s+'s':'');
return str ? str : "0s";
}
c1.onTick = function () { doLog("tick! current value = "+this.currentValue); }
c1.onStart = function () { doLog("start : from "+this.startValue+" to "+this.stopValue+" by "+this.step); }
c1.onStop = function () { doLog("<b>stop! final value = "+this.currentValue+"</b>"); }
</script>
<!-- Mon second chrono -->
<script>
var c2 = new Chrono(10, 0, -1, 1000, "chrono2");
c2.onStop = function () { this.writeToLayer("BOUM !"); }
</script>
</head>
<body>
<fieldset>
<legend><strong>Chrono 1 (loggй) : 0 а 2000, interval = 1/10s</strong></legend>
<p>Clic = pause/continue. Double-clic = stop/start.</p>
<a href="#" ondblclick="if (c1.running) c1.stop(); else c1.start();" onclick="if (c1.running) c1.pause(); else c1.unpause();" id="chrono1">Lancer/arrкter le chrono</a>
<div id="log"></div>
</fieldset>
<fieldset>
<legend><strong>Chrono 2 (comptes а rebours) : 10 а 0, interval = 1s</strong></legend>
<a href="#" onclick="c2.start()" id="chrono2">Lancer le compte а rebours</a>
</body>
</html>
[/code]