Posted at

一定時間ごとに関数を実行するsetInterval

More than 3 years have passed since last update.

<!doctype html>

<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<script>
// タイムアウトはms単位
var timeout = 1000;

var counter = 0;

intervalID = setInterval( function () {
counter = counter + (timeout / 1000);
// ここにfunction
document.getElementById("count").innerHTML= counter + "秒経過";
console.log(counter + "秒経過");
}, timeout );
console.clear();
</script>
<div id="count"></div>

<button onclick="clearInterval(intervalID)">停止</button>

</body>
</html>