这是JS代码 这里函数stopi不起作用。什么问题 ? / * 理想情况下,您可以在代码块之外添加尽可能多的说明和上下文,因为您不再局限于混合代码和有关代码的元信息。
通过粗略检查您的屏幕截图,评论似乎由TA或讲师插入,作为您正在完成的课程的一部分,并且可能与您的问题无关。删除它们,并在代码块之外汇总必要的详细信息。 * /
function fun(){
var stopping = setInterval(_fun,1500); // interval
}
function _fun(){
var i = Math.floor((Math.random()*9)+1);
//var doc = document.getElementById(""+i+"");
document.getElementById("pp").innerHTML = i ;
switch(i)
{
case 1:
{
if(document.getElementById("1").bgColor == "orange")
{
document.getElementById("1").bgColor = "red";
break;
}
else if(document.getElementById("1").bgColor == "yellow"){
document.getElementById("1").bgColor = "orange";
break;
}
else
document.getElementById("1").bgColor = "yellow";
break;
}
case 2:
{
if(document.getElementById("2").bgColor == "yellow"){
document.getElementById("2").bgColor = "orange";
break;
}
else
document.getElementById("2").bgColor = "yellow";
break;
}
case 3:
{
if(document.getElementById("3").bgColor == "yellow"){
document.getElementById("3").bgColor = "orange";
break;
}
else
document.getElementById("3").bgColor = "yellow";
break;
}
case 4:
{
if(document.getElementById("4").bgColor == "yellow"){
document.getElementById("4").bgColor = "orange";
break;
}
else
document.getElementById("4").bgColor = "yellow";
break;
}
case 5:
{
if(document.getElementById("5").bgColor == "yellow"){
document.getElementById("5").bgColor = "orange";
break;
}
else
document.getElementById("5").bgColor = "yellow";
break;
}
case 6:
{
if(document.getElementById("6").bgColor == "yellow"){
document.getElementById("6").bgColor = "orange";
break;
}
else
document.getElementById("6").bgColor = "yellow";
break;
}
case 7:
{
if(document.getElementById("7").bgColor == "yellow"){
document.getElementById("7").bgColor = "orange";
break;
}
else
document.getElementById("7").bgColor = "yellow";
break;
}
case 8:
{
if(document.getElementById("8").bgColor == "yellow"){
document.getElementById("8").bgColor = "orange";
break;
}
else
document.getElementById("8").bgColor = "yellow";
break;
}
case 9:
{
if(document.getElementById("9").bgColor == "yellow"){
document.getElementById("9").bgColor = "orange";
break;
}
else
document.getElementById("9").bgColor = "yellow";
break;
}
}
}
function stopi(){
clearInterval(stopping);
}
这是html代码
<html>
<body>
<script src="New.js">
</script>
<p id="pp"></p>
<button onclick="fun()"> Start </button>
<button onclick="stopi()"> Cancel </button>
<br>
<br>
<table border="1" style="width:15%">
<tr>
<td id="1">1</td>
<td id="2">2</td>
<td id="3">3</td>
</tr>
<tr>
<td id="4">4</td>
<td id="5">5</td>
<td id="6">6</td>
</tr>
<tr>
<td id="7">7</td>
<td id="8">8</td>
<td id="9">9</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:2)
function fun(){
var stopping = setInterval(_fun,1500); // interval
}
停止是fun()
函数的局部变量。将其声明为全局,以便您可以从其他函数访问它
var stopping;
function fun(){
stopping = setInterval(_fun,1500); // interval
}
function stopi(){
clearInterval(stopping);
}
答案 1 :(得分:1)
您必须在函数外声明stopping
变量:
var stopping;
function fun(){
stopping = setInterval(_fun,1500); // interval
}