yoshi blog

IT infra engineer working at Tokyo, Japan. coding on weekends just for fun.

Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Sunday, June 25, 2017

meeting timer

meeting timer
mtg_timer

you can also see remain time on the tab.
TimeKeeper

current time is

end time is

remain time is
min sec



<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <!--multi device--!>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <title>TimeKeeper</title>
    </head>
    <body>
    <!-- get current time --!>
    <p>
    current time is<label id="RealtimeClock"></label>
    </p>
    <!-- get end time --!>
    <p>
    end time is<input type="text"id="EndTime"  value="18:00" size="10">
    </p>
    <!-- get remain time and set buttons --!>
    <p>
    <form name="timer">
    remain time is</br>
    <input type="text" value="10" size="10">min
    <input type="text" value="0" size="10">sec<br>
    <input type="button" value="start" onclick="cntStart()">
    <input type="button" value="stop" onclick="cntStop()">
    <input type="button" value="reset" onclick="reSet()">
    </form>
    <!-- get aleart message --!>
    <font color="red"><label id="AlertMessage"></label></font>
    </p>
    <!-- get textbox --!>
    <p>
    <textarea name="freeans" rows="20" cols="30">Today's Agenda</textarea>
    </p>
        <!-- javascript --!>
        <script type="text/javascript">
        // display clock
        function showClock() {
           var nowTime = new Date();
           var nowHour = setfig( nowTime.getHours() );
           var nowMin = setfig( nowTime.getMinutes() );
           var nowSec = setfig( nowTime.getSeconds() );
           var msg = nowHour + ":" + nowMin + ":" + nowSec;
           document.getElementById("RealtimeClock").innerHTML = msg;
        }
        setInterval('showClock()',1000);
        // fix number of digits to two if its one
        function setfig(num) {
           var ret;
           if( num < 10 ) { ret = "0" + num; }
           else { ret = num; }
           return ret;
        }
        // set timer id
        var timer1;
        
        // get countdown timer for 1000msec
        function cntStart() {
          document.timer.elements[2].disabled=true;
          timer1=setInterval("countDown()",1000);
        }
        
        // stop timer
        function cntStop() {
          document.timer.elements[2].disabled=false;
          clearInterval(timer1);
        }
        
        // countdowm timer
        function countDown() {
          var min=document.timer.elements[0].value;
          var sec=document.timer.elements[1].value;
          
          if( (min=="") && (sec=="") ) {
            var msg="set time!";
            document.getElementById("AlertMessage").innerHTML = msg;
            reSet();
          } else {
            if (min=="") min=0;
            min=parseInt(min);
            
            if (sec=="") sec=0;
            sec=parseInt(sec);
            
            tmWrite(min*60+sec-1);
          }
        }
        
        // get remain time
        function tmWrite(int) {
          int=parseInt(int);
          
          if (int<=0)
          {
            reSet();
            var msg="it's time!";
            //alert(msg)
            document.getElementById("AlertMessage").innerHTML = msg;
            // aleart tab if remain time is zero
            document.title  = msg
          } else {
            // get remain min
            rem_min=Math.floor(int/60);
            // get remain sec
            rem_sec=int % 60;
            // get remain time
            document.timer.elements[0].value=rem_min;
            document.timer.elements[1].value=rem_sec;
            // display remain time
            document.title  = 'remain time is ' + rem_min + ':' + rem_sec;
          }
        }
        
        // reset form
        function reSet() {
          var msg="";
          document.getElementById("AlertMessage").innerHTML = msg;
          document.timer.elements[0].value="10";
          document.timer.elements[1].value="0";
          document.timer.elements[2].disabled=false;
          clearInterval(timer1);
        }
        </script>
    </body>
</html>
Posted by yoshi at 4:32 PM
Labels: javascript

Sunday, May 28, 2017

make sound with javascript

javascript can make sound. I tried as a first javascript coding. the sound is from the famous Japanese movie :)

push the button to play music. *check the system volume!!
Audio API test

sample code is below.
<!DOCTYPE html>
<html lang="ja">
     <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

          <title>Audio API test</title>

          <!-- Bootstrap core CSS -->
          <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" id="themesid">

          <!-- Custom styles -->
          <link href="https://getbootstrap.com/examples/theme/theme.css" rel="stylesheet">
          <link href="https://getbootstrap.com/dist/css/bootstrap-theme.min.css" rel="stylesheet">
          <link href="https://getbootstrap.com/examples/offcanvas/offcanvas.css" rel="stylesheet">

     </head>

     <body>
          <div class="container">
               <div class="row row-offcanvas row-offcanvas-right">
                    <div class="col-xs-12 col-sm-9">
                         <div class="row">

                              <!-- button -->
                              <div class="container theme-showcase" role="main">
                                   <p>
                                        <button type="button" class="btn btn-lg btn-primary" id="PlayButton">Play</button>
                                   </p>
                              </div>

                         </div>
                    </div>
               </div>
          </div>

          <script>
               function play() {

                    window.AudioContext = window.AudioContext || window.webkitAudioContext; // cross browser
                    var audioCtx = new AudioContext();

          for (key = 0; key < music.length; key++) {

                         // define sound key
                         if(music[key] == "Do"){var hz = 262;}
                         if(music[key] == "re"){var hz = 294;}
                         if(music[key] == "mi"){var hz = 330;}
                         if(music[key] == "fa"){var hz = 349;}
                         if(music[key] == "so"){var hz = 392;}
                         if(music[key] == "la"){var hz = 440;}
                         if(music[key] == "si"){var hz = 494;}
                         if(music[key] == "Doo"){var hz = 523;}
                         if(music[key] == "ree"){var hz = 587;}
                         if(music[key] == "mii"){var hz = 659;}
                         if(music[key] == "faa"){var hz = 699;}
                         if(music[key] == "soo"){var hz = 784;}
                         if(music[key] == "laa"){var hz = 880;}
                         if(music[key] == "sii"){var hz = 988;}

                         // sound play
                         if(hz != "none"){
                              var osciillator = audioCtx.createOscillator();   // sound generate
                              osciillator.frequency.value = hz;   // defined hertz 
                         var audioDestination = audioCtx.destination;   //defined sound output
                         osciillator.connect(audioDestination);   // connect speaker
                         osciillator.start = osciillator.start || osciillator.noteOn; // cross browser
                         osciillator.start();  // start playing
                              osciillator.stop(audioCtx.currentTime + time[key]);   // stop sound after time[key] sec
                         }

                         // sleep between each sound
                         Sleep(time[key]+0.1);

                    }
               }

               // sleep function
               function Sleep(T){
                  var d1 = new Date().getTime();
                  var d2 = new Date().getTime();
                  while( d2 < d1+1000*T ){     // wait T sec
                       d2=new Date().getTime();
                  }
                  return;
               }

               // defined tone
               var music = [];
               music.push("soo");
               music.push("mii");
               music.push("Doo");
               music.push("so");
               music.push("so");
               music.push("so");
               music.push("la");
               music.push("Doo");
               music.push("faa");
               music.push("laa");
               music.push("soo");

               // defined length of sound
               var time = [];
               time.push(0.7);
               time.push(0.2);
               time.push(0.7);
               time.push(0.2);
               time.push(0.2);
               time.push(0.2);
               time.push(0.2);
               time.push(0.2);
               time.push(0.2);
               time.push(0.2);
               time.push(0.7);

               // execution
               var Button1 = document.getElementsByClassName("btn btn-lg btn-primary");
               Button1[0].addEventListener("click", play, false);

          </script>
     </body>
</html>
Posted by yoshi at 2:15 AM
Labels: html, javascript
Older Posts Home
Subscribe to: Posts (Atom)

About Me

IT infra engineer working at Tokyo. want to improve english skill.

Labels

python mac ansible aws blogger html javascript shellscript apache bitnami go linux nginx ssl ubuntu vagrant virtualbox vmware

Line stamp

Line stamp
Line stamp I made is here. I hope you like them!

tip me for more fun!

15zgMbiE1aSwGvWxTzTjtoiLN5FrRwqpKv Donate with IndieSquare

links

  • github
  • line
  • blog(Japanese)

twitter

Tweets by yoshiisland_dev

Search This Blog

Labels

  • ansible (2)
  • apache (1)
  • aws (2)
  • bitnami (1)
  • blogger (2)
  • go (1)
  • html (2)
  • javascript (2)
  • linux (1)
  • mac (3)
  • nginx (1)
  • python (5)
  • shellscript (2)
  • ssl (1)
  • ubuntu (1)
  • vagrant (1)
  • virtualbox (1)
  • vmware (1)

Blog Archive

  • ▼  2019 (1)
    • ▼  December (1)
      • How to restart apache with Bitnami EC2?
  • ►  2018 (2)
    • ►  April (1)
    • ►  January (1)
  • ►  2017 (14)
    • ►  December (1)
    • ►  July (1)
    • ►  June (1)
    • ►  May (11)

Pages

  • Home
Simple theme. Powered by Blogger.