Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="timer">
  <h2>タイマー</h2>
  <div class="timer-input">
    <input type="text" value="0" />秒
  </div>
  <p id="js-timerTime">00:00:00</p>
  <input id="js-timerStart" type="button" value="開始" />
  <input id="js-timerStop" type="button" value="停止" disabled />
  <input id="js-timerResset" type="button" value="リセット" disabled />
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                // ■カウントダウンタイマー
const $timer=document.getElementById('js-timerTime');
const $tStart = document.getElementById('js-timerStart');
const $tStop =document.getElementById('js-timerStop');
const $tResset =document.getElementById('js-timerResset');

// 入力値を取得
const $input = document.querySelector('.timer-input input');

// ターゲット時間
let tTarget; //残り時間
let tStartTime; //開始時刻
let tStopTime=0; //停止時間
let tCountId ; //インターバル格納
let setTime; //カウントダウン設定時間

// ターゲット時間をセットし描画
function displayCount(){
  tTarget = new Date(tStartTime+(1000*setTime)-Date.now()-tStopTime);
  let ch = String(tTarget.getHours()-9).padStart(2,'0');
  let cm = String(tTarget.getMinutes()).padStart(2,'0');
  let cs = String(tTarget.getSeconds()).padStart(2,'0');
  if(cs==0){
    tResset();
    alert('終了です');
  }else{
    $timer.innerHTML=(`${ch}:${cm}:${cs}`);
    tCountId = setTimeout(displayCount,500);
  }
}

// スタートボタンを押したとき
$tStart.addEventListener('click',()=>{
  $tStart.disabled = true;
  $tStop.disabled = false;
  $tResset.disabled = true;
  setTime =$input.value;
  tStartTime = Date.now();
  displayCount();
});

// ストップボタンを押したとき
$tStop.addEventListener('click',()=>{
  $tStart.disabled = false;
  $tStop.disabled = true;
  $tResset.disabled = false;
  clearTimeout(tCountId);
  tStopTime +=Date.now()-tStartTime;
});

// リセットボタンが押されたとき
$tResset.addEventListener("click", function () {
  tResset();
});

function tResset(){
  $tStart.disabled = false;
  $tStop.disabled = true;
  $tResset.disabled = true;
  $timer.textContent = "00:00:00";
  tStopTime = 0;  
}
              
            
!
999px

Console