我正在尝试为每秒将文本替换为div的程序设置一个“停止”按钮。
以下是代码:
var Timer=setInterval(function(){first()}, 1000);
function first()
{
var letter = document.getElementById("letter");
var notes = ["A", "B", "C", "D", "E", "F", "G"];
var quality1 = ["#", "b", " ", " "];
var randomNotes = notes[Math.floor(Math.random() * notes.length)];
var randomQuality1 = quality1[Math.floor(Math.random() * quality1.length)];
var all = randomNotes + randomQuality1;
letter.innerHTML = all;
letter.replace("all","all");
}
function stop()
{
clearInterval(Timer);
}
document.addEventListener("DOMContentLoaded", first, false);
我对编程比较陌生,但我找不到解决方案!
答案 0 :(得分:0)
您的代码运行正常。以下是如何将其实现为HTML的示例:
<script>
var Timer;
window.onload=function(){Timer=setInterval(function(){first()}, 1000);}
function first(){
var letter = document.getElementById("letter");
var notes = ["A", "B", "C", "D", "E", "F", "G"];
var quality1 = ["#", "b", " ", " "];
var randomNotes = notes[Math.floor(Math.random() * notes.length)];
var randomQuality1 = quality1[Math.floor(Math.random() * quality1.length)];
var all = randomNotes + randomQuality1;
letter.innerHTML = all;
letter.replace("all","all");
}
function stop(){
clearInterval(Timer);
}
</script>
<input type="button" onclick="stop()" value="STOP"/>
<div id="letter"></div>