Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <label id="display">還有 5 秒</label>
  <button id="button">開始倒數</button>
</body>
</html>
<script>
  const display = document.getElementById("display")
  const button = document.getElementById("button")
  let countDownSec = 5
  let timer
  
  button.onclick = ()=>{
    toggle()
  }
  
  function startCountDown(){
    button.innerText = "暫停倒數"
    timer = setInterval(()=>{
      countDownSec = countDownSec -1
      display.innerText = `還有 ${countDownSec} 秒`
      if (countDownSec == 0) {
        pause()
        countDownSec = 5
        display.innerText = `還有 ${countDownSec} 秒`
      }
    }, 1000)
  }
  
  function pause(){
    clearInterval(timer)  
    button.innerText = "開始倒數"
  }
  
  function toggle(){
    if (button.innerText == "開始倒數") {
      startCountDown()
    } else {
      pause() 
    }
  }
</script>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers