Posto uno script fatto tanto per esercitazione che gestisce un countdown fatto da giorni/ore/minuti/secondi con un effetto sonoro funzionante correttamente solo su chrome, puo sempre essere fonte di spunto
pagina.html
timer_s.js
pagina.html
Codice:
<script src="timer_s.js"></script>
<div id="timer" style="margin-top:20%;font-family:Arial,Helvetica,sans-serif;text-align:center;font-size:30px;"></div>
<script>
timer.CallTimer(
new Array(2,4,34,20),
document.getElementById('timer'),
'clock.mp3',
'MyCallBackFunction()'
);
function MyCallBackFunction(){ alert('Finish'); }
</script>
timer_s.js
Codice:
/*
* Sound Timer
*/
var timer = {
times : null,
div : null,
sound: null,
func: null,
Stop: false,
CallTimer: function(time,div,sound,func){
this.times = time;
this.checkSet();
this.div = div ? div : null;
this.sound = sound ? sound : null;
this.func = func ? func : null;
this.StartTimer();
},
checkSet: function(){
for(i=0;i<4;i++)
this.times[i] = typeof this.times[i] === 'number' ? parseInt(this.times[i]) : 0;
},
StartTimer: function(){
this.SpawnTextAndSound();
this.checkStatus();
if(!this.Stop)
setTimeout('timer.StartTimer()',1000);
},
SpawnTextAndSound : function(){
this.div.innerHTML = this.times[0]+' : '+this.times[1]+' : '+this.times[2]+' : '+this.times[3]+'<embed src=\''+this.sound+'\' hidden=true autostart=true>';
},
checkStatus : function(){
if(this.times[3]==0 && this.times[2]==0 && this.times[1]==0 && this.times[0]==0){ setTimeout(this.func,0); this.Stop = true; }
else if(this.times[3]==0 && this.times[2]>0){ this.times[3]=60; this.times[2]--; }
else if(this.times[3]==0 && this.times[2]==0 && this.times[1]>0){ this.times[3]=60; this.times[2]=59; this.times[1]--; }
else if(this.times[3]==0 && this.times[2]==0 && this.times[1]==0 && this.times[0]>0){ this.times[3]=60; this.times[2]=59; this.times[1]=24; this.times[0]--; }
else this.times[3]--;
}
}
Ultima modifica: