看看这段代码哪错了?

qq277049 2011-11-25 04:07:06
<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
window.clearInterval(mytime);//取消定时
}
function g(){
var mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}

function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);

//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];

}

</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s" value='开始' onclick="g()"><input type="button" id="s" value='停止' onclick="s()">
</form>
</body>
</html>
...全文
63 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
默默不得鱼 2011-11-25
  • 打赏
  • 举报
回复
另外 function g() {
里 要把var去掉 不然外面的mytime还是空的
默默不得鱼 2011-11-25
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
var mytime=null;
function ss() {
window.clearInterval(mytime);//取消定时
}

function g() {
mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}

function myshow() {
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);

//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];

}
</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s" value='开始' onclick="g()">
<input type="button" id="a" value='停止' onclick="ss()">
</form>
</body>
</html>

你的方法名和按钮的ID冲突了
p2227 2011-11-25
  • 打赏
  • 举报
回复
<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
window.clearInterval(mytime);//取消定时
}
function g(){
mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
/////////////////////不需要再var/////////////////////////////////////////////////
}

function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);

//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];

}

</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s1" value='开始' onclick="g()"><input type="button" id="s2" value='停止' onclick="s()"><!--id不能重复-->
</form>
</body>
</html>



hookee 2011-11-25
  • 打赏
  • 举报
回复
1 mytime 前面不要var
2 id不要和函数重名 s

<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
clearInterval(mytime);//取消定时
}
function g(){
mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}

function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);

//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];

}

</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="cs" value='开始' onclick="g()"><input type="button" id="cs" value='停止' onclick="s()">
</form>
</body>
</html>

qq277049 2011-11-25
  • 打赏
  • 举报
回复
只能开始,不能停止


<html>
<head>
<script type="text/javascript">
var mytime=null;
function s(){
window.clearInterval(mytime);//取消定时
}
function g(){
var mytime = setInterval("myshow()",30);//定义每隔30毫秒执行一次
}

function myshow(){
//获取一个随机数
var r = Math.random();
var x = Math.floor(r*10000000);

//随机从name数组中获取一个名字
var name=['张三','李四','王五','赵六','田七'];
var i = x%(name.length);
document.getElementById("mydiv").innerHTML=name[i];

}

</script>
</head>
<body>
<h2>JavaScript语言的Math对象</h2>
<div id="mydiv" style="width:150px;height:100px; line-height:100px;background:#cccccc;color:#ff6600;font-size:40px;">
张三
</div>
<form>
<input type="button" id="s" value='开始' onclick="g()"><input type="button" id="s" value='停止' onclick="s()">
</form>
</body>
</html>


87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧