The Astral Pulse

The Astral Library => Welcome to Writers Corner! => Topic started by: Smertrios on November 05, 2018, 17:11:35



Title: javascript to play an audio file every 20 minutes... after initial 4 hour delay
Post by: Smertrios on November 05, 2018, 17:11:35
Maybe this counts as "writing"... saw in another thread someone was looking to play an audio file every 20 minutes after an initial 4 hour delay. This was my attempt at it...

Code:
<!DOCTYPE html>
<!--
Author: Smertrios
Date: November 5, 2018
Purpose: javascript for delays and sound files. this script
(after editing) will wait 4 hours then start playing an audio
file every 20 minutes. check the comments for information on
how to edit.

Note: its more complicated than it looks because of "policies"
which are basically hidden rules secretely imposed on new
scripters to make life more difficult by forcing the scripter
to search for reasons why the "simple" script they have typed
in is not working.

For instance I had to add a start button so there would be
"interaction with the page" before playing a sound file and how
the element reference returned by getElementById was defined
at the "global" level before it could be used in a function.

Rumor has it that "one does not simply play... an audio file"

How to use this script?

create a directory with this file saved as test.html and 2 other
audio files named testing.wav and audio1.wav. When you open
test.html in your browser and click the start button it will
play testing.wav to show that audio is working and then start
playing audio1.wav every 20 minutes (after a 4 hour delay *and*
after this file has had the delays I used for testing changed)
-->
<html>
<body>

<button onclick="fn0()" type="button">Start</button> Reload page to "STOP"<br>

<h1 id="timer">Time: 0:0:0</h1>

<audio id="testing" controls>
<source src="testing.wav" type="audio/wav">
</audio>

<audio id="audio1" controls>
<source src="audio1.wav" type="audio/wav">
</audio><br>

<script>
var counter=0;

var hours=function(t){return t*minutes(60);}
var minutes=function(t){return t*seconds(60);}
var seconds=function(t){return t*1000;}

var testing=document.getElementById("testing");
var fn0=function(){
// change to hours(4) for 4 hour delay
setTimeout(fn1,seconds(5));
counter++;
document.body.innerHTML+=counter+" initial 4 hour delay started<br>"
// make sure audio is working by playing a "test" sound
testing.play();
counter++;
document.body.innerHTML+=counter+" playing \"testing\" sound file<br>";
// START TIMER
setInterval(onTimer,1000);
}

var fn1=function(){
setTimeout(fn2,0);
counter++;
document.body.innerHTML+=counter+" initial 4 hour delay finished<br>"
}

var audio1=document.getElementById("audio1");
var fn2=function(){
// change to minutes(20) for a 20 minute delay
setTimeout(fn2,seconds(5));
audio1.play();
counter++;
document.body.innerHTML+=counter+" playing \"audio1\" every 20 minutes<br>"
}

var Timer=0;
var onTimer=function()
{
Timer+=1;
var temp=Timer;
var hours=Math.floor(temp/3600);
temp-=hours*3600;
var minutes=Math.floor(temp/60);
temp-=minutes*60;
var seconds=temp;
var ele=document.querySelector('#timer');
ele.innerHTML='Time: '+hours+':'+minutes+':'+seconds;
}
</script>
</body>
</html>

Do you think coding subforum would have a place here?