• javascript 代码
function countTime(dif) {
  if (dif >= 0) {
    let m = Math.floor(dif / 60 % 60);
    let s = Math.floor(dif % 60);

    m = m.toString()[1] ? m : '0' + m;
    s = s.toString()[1] ? s : '0' + s;

    console.log(m + '分' + s + '秒');

    setTimeout(countTime, 1000, --dif);
  } else {
    console.log('倒计时结束');
  }
}

// 参数秒
countTime(2 * 60);
  • 全部html代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>js分秒倒计时</title>
</head>
<body>
<center>
  <span id="m">00</span>
  <span id="s">00</span>
</center>

<pre>
  <code></code>
</pre>
<script type="text/javascript">
  function countTime(dif) {
    if (dif >= 0) {
      let m = Math.floor(dif / 60 % 60);
      let s = Math.floor(dif % 60);

      m = m.toString()[1] ? m : '0' + m;
      s = s.toString()[1] ? s : '0' + s;

      document.getElementById("m").innerHTML = m + "分";
      document.getElementById("s").innerHTML = s + "秒";

      setTimeout(countTime, 1000, --dif);
    } else {
      console.log('倒计时结束');
    }
  }

  // 参数秒
  countTime(2 * 60);
  
  document.getElementsByTagName("code")[0].innerHTML = document.getElementsByTagName("script")[0].innerHTML;

</script>
</body>
</html>