set a sound on a kitchen timer

Asked 1 years ago, Updated 1 years ago, 37 views

I have found a sample code that displays an alert screen after a set time, but how do I rewrite the code to make a sound?

The sample code is from the following site.
http://www.pori2.net/js/timer/6.html

Additional information: December 21, 2017

Thank you for your advice. I rewritten it as below, but there was no sound.
Is there something wrong?

<script>
vartimer1;// Declare the variable (timer ID) that stores the timer

// A function that calls the countdown function every 1000 milliseconds
function cntStart()
{
  document.timer.elements[2].disabled=true;
  timer1 = setInterval ("countDown(), 1000);
}

// timer stop function
function cntStop()
{
  document.timer.elements[2].disabled=false;
  clearInterval (timer1);
}

// Countdown function
function countDown()
{
  varmin = document.timer.elements[0].value;
  varsec = document.timer.elements[1].value;

  if((min=="")&&(sec=="")
  {
    alert("Please set the time!");
    reSet();
  }
  else
  {
    if(min=="")min=0;
    min = parseInt(min);

    if(sec=="")sec=0;
    sec = parseInt(sec);

    tmWrite(min*60+sec-1);
  }
}

// Function to write the remaining time
function tmWrite(int)
{
  int = parseInt(int);

  if(int<=0)
  {
    reSet();
    new Audio("hoge.mp3");
    play();
  }
    else
  {
    // Cut the remaining fraction by dividing int by 60
    document.timer.elements[0].value=Math.floor(int/60);
    // The number of seconds remaining is after int divided by 60
    document.timer.elements[1].value=int%60;
  }
}

// The function of returning the form to its initial state (reset)
function reset()
{
  document.timer.elements[0].value="0";
  document.timer.elements[1].value="0";
  document.timer.elements[2].disabled=false;
  clearInterval (timer1);
}  
</script>

javascript

2022-09-30 21:29

1 Answers

I can't read the code without looking at it carefully, but if you look at it roughly, it's alert("time!" in the if statement in the tmWrite function!"); What about the code that makes the sound?
The audio file you want to ring has mp3, wav extensions.
This is an easy way to do it

new Audio("path");
play();

will be .
Other than play(), you can stop at stop() or loop at loop().
If you enter a number in the loop function argument, you loop as many times as you want, or you loop endlessly.
Play() automatically stops when the sound/music is finished.
I can't do anything too elaborate.
http://webos-goodies.jp/archives/50855398.html

And I don't use it, but I can do something a little elaborate.
I'll just post the link.
https://syncer.jp/html5-javascript-hello-button

The browser that mp3 can play is from 2010 to 2014.Want to know about wav or something else?
https://caniuse.com/#
See for more information on other features.(I remember)


2022-09-30 21:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.