
javascript中如何通过一个按钮来实现开始和停止的功能?
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>choujiang</title>
<script>
var time=null;
var list="500万,直升飞机,宇宙飞船,豪华轿车,别墅".split(",");
function startRun(){
var i=Math.random()*list.length;
i=Math.floor(i);
document.getElementById("award").innerText=list[i];
time=setTimeout("startRun()",100);
}
function stopRun(){
if(time!=null)
clearTimeout(time);
}
</script>
</head>
<body>
恭喜您获得的奖品:
<div id="award" style="font-size:100px; color:red">
</div>
<input type="button" value="开始" onclick="startRun()">
<input type="button" value="停止" onclick="stopRun()">
</body>
</html>
代码是上面那样子的 我想改成变成一个按钮来实现开始和停止的功能,应该怎么改?本来我是通过定义按钮的名字的 点击一下按钮,它的的值就会从开始变成停止,然后执行 clearTimeout(time);但这样子后 我运行时一点击就开始按钮后它就停了下来。到底应该怎么实现此功能啊 望各位高手指点下. 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>choujiang</title>
<script>
var time=null;
var list="500万,直升飞机,宇宙飞船,豪华轿车,别墅".split(",");
function startRun(){
var i=Math.random()*list.length;
i=Math.floor(i);
document.getElementById("award").innerText=list[i];
time=setTimeout("startRun()",100);
}
function stopRun(){
if(time!=null)
clearTimeout(time);
}
</script>
</head>
<body>
恭喜您获得的奖品:
<div id="award" style="font-size:100px; color:red">
</div>
<input type="button" value="开始" onclick="startRun()">
<input type="button" value="停止" onclick="stopRun()">
</body>
</html>
代码是上面那样子的 我想改成变成一个按钮来实现开始和停止的功能,应该怎么改?本来我是通过定义按钮的名字的 点击一下按钮,它的的值就会从开始变成停止,然后执行 clearTimeout(time);但这样子后 我运行时一点击就开始按钮后它就停了下来。到底应该怎么实现此功能啊 望各位高手指点下. 展开
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>choujiang</title>
<script>
var time=null;
var list="500万,直升飞机,宇宙飞船,豪华轿车,别墅".split(",");
var bol = false;
function startRun(){
var i=Math.random()*list.length;
i=Math.floor(i);
document.getElementById("award").innerText=list[i];
document.getElementById("cc").value="停止";
time=setTimeout("startRun()",100);
}
function stopRun(){
if(time!=null)
document.getElementById("cc").value="开始";
clearTimeout(time);
}
function startorstop(){
if(bol)
stopRun();
else
startRun();
bol = !bol;
}
</script>
</head>
<body>
恭喜您获得的奖品:
<div id="award" style="font-size:100px; color:red">
</div>
<input type="button" id="cc" value="开始" onclick="startorstop()">
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>choujiang</title>
<script>
var time=null;
var list="500万,直升飞机,宇宙飞船,豪华轿车,别墅".split(",");
var bol = false;
function startRun(){
var i=Math.random()*list.length;
i=Math.floor(i);
document.getElementById("award").innerText=list[i];
document.getElementById("cc").value="停止";
time=setTimeout("startRun()",100);
}
function stopRun(){
if(time!=null)
document.getElementById("cc").value="开始";
clearTimeout(time);
}
function startorstop(){
if(bol)
stopRun();
else
startRun();
bol = !bol;
}
</script>
</head>
<body>
恭喜您获得的奖品:
<div id="award" style="font-size:100px; color:red">
</div>
<input type="button" id="cc" value="开始" onclick="startorstop()">
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询