Questo l'ho riadattato da
http://forum.html.it/forum/showthrea...readid=1278547
codice:
<script type="text/javascript">
// <![CDATA[
function $id(id) {
return document.getElementById(id);
}
var countdown = function(seconds, formid, callback) {
var _ore = $id(formid + '_ore');
var _min = $id(formid + '_min');
var _sec = $id(formid + '_sec');
var _writecounter = function(s) {
var hh = Math.floor(s / 3600);
var mm = Math.floor((s - (hh*3600)) / 60);
var ss = s - (hh*3600) - (mm*60);
_ore.value = (hh < 10)? "0" + hh : hh;
_min.value = (mm < 10)? "0" + mm : mm;
_sec.value = (ss < 10)? "0" + ss : ss;
};
(function() {
_writecounter(seconds);
while (seconds--) return setTimeout(arguments.callee, 1000)
return callback();
})();
};
// ]]>
</script>
<body onload="(function() {
countdown(10, 'cd1', function() { alert('ho finito il primo countdown'); });
countdown(86, 'cd2', function() { alert('ho finito anche il secondo'); });
})();">
<form action="#" id="cd1">
<input type="text" size="2" id="cd1_ore" readonly="readonly" /> :
<input type="text" size="2" id="cd1_min" readonly="readonly" /> :
<input type="text" size="2" id="cd1_sec" readonly="readonly" />
</form>
<form action="#" id="cd2">
<input type="text" size="2" id="cd2_ore" readonly="readonly" /> :
<input type="text" size="2" id="cd2_min" readonly="readonly" /> :
<input type="text" size="2" id="cd2_sec" readonly="readonly" />
</form>
</body>
</html>
la funzione countdown accetta come argomenti
- i secondi di countdown;
- l'id del form che contiene i tre campi ore, minuti e secondi (i quali devono avere id
<form_id>_ore
<form_id>_min
<form_id>_sec
);
- una funzione di callback da eseguire (che può essere anche vuota) al termine del countdown;
Ciao