当前位置 - 股票行情交易網 - 企業服務 - js定時器

js定時器

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<button type="button" id="cancel">取消定時器</button>

<script>

let time = setInterval(()=>{

console.log('當前時間是:'+timer())

},1000)

function timer(){

let date = new Date();

let hour = date.getHours().toString().padStart(2,"0");

let min = date.getMinutes().toString().padStart(2,"0");

let sec = date.getSeconds().toString().padStart(2,"0");

return `${hour}:${min}:${sec}`

}

document.getElementById('cancel').onclick = function(){

clearInterval(time)

alert('已取消定時器')

}

</script>

</body>

</html>