The Astral Pulse
News: http://www.astralpulse.com/aup.html. Acceptable Use Policy for the forums. Please read and ensure that you respect these policies. Thank you.

Please note that due to the amount of spam posts we have been receiving over the past few months, we have switched Registration to require you to be approved by a moderator.  We will go through the approval list as often as we can, but if it's been 24 hours and you haven't been Approved yet or you've received a rejection email, please email myself or one of the moderators immediately so we may correct the application.

We apologize for any inconveniences this may cause, but it's the last resort we have to fighting the spam for now.
 
*
Welcome, Guest. Please login or register. December 14, 2018, 15:22:39


Login with username, password and session length


Pages: [1]   Go Down
  Print  
Author Topic: javascript to play an audio file every 20 minutes... after initial 4 hour delay  (Read 438 times)
Smertrios
Astral Energy 1
*
Offline Offline

Posts: 8


View Profile Email
« 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?
Logged
The Astral Pulse
   



 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
SMFAds for Free Forums


The Astral Pulse Copyright 2002 - 2014
Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM