首页 新闻 赞助 找找看

一个简单的抽奖,setinterval为什么清除不了呢?

0
[已解决问题] 解决于 2017-03-17 21:13

html

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/js学习2.js"></script>
        <link rel="stylesheet" type="text/css" href="css/js学习2.css" />
    </head>

    <body>
        <h1>手机号码摇奖</h1>
        <div class="c1">
            <div id="d1" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d2" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d3" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d4" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d5" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d6" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d7" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d8" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d9" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d10" style="width: 50px;height: 50px;border: solid 1px black;"></div>
            <div id="d11" style="width: 50px;height: 50px;border: solid 1px black;"></div>
        </div>
        <div style="clear: left;padding-top:50px ;padding-left: 200px;">
            <input type="button" value="开始" onclick="Start()">
            <input type="button" value="停止" onclick="Stop()">
            <input type="button" value="清除" onclick="Clear()">
            
        </div>
    </body>

</html>

css

.c1 div {
    float: left;
}

js

function Start() {
    var a=setInterval(function() {
        document.getElementById("d1").innerText = Math.round((Math.random() * 9));
        document.getElementById("d2").innerText = Math.round((Math.random() * 9));
        document.getElementById("d3").innerText = Math.round((Math.random() * 9));
        document.getElementById("d4").innerText = Math.round((Math.random() * 9));
        document.getElementById("d5").innerText = Math.round((Math.random() * 9));
        document.getElementById("d6").innerText = Math.round((Math.random() * 9));
        document.getElementById("d7").innerText = Math.round((Math.random() * 9));
        document.getElementById("d8").innerText = Math.round((Math.random() * 9));
        document.getElementById("d9").innerText = Math.round((Math.random() * 9));
        document.getElementById("d10").innerText = Math.round((Math.random() * 9));
        document.getElementById("d11").innerText = Math.round((Math.random() * 9));
    }, 100);
}

function Clear(){
    clearInterval(a);
}

function Stop(){
    
}

为什么Clear()清除不了?

以及用什么方法让它暂定而不清除呢?谢谢大神

爽7的主页 爽7 | 菜鸟二级 | 园豆:204
提问于:2017-03-15 19:39
< >
分享
最佳答案
1

把var a 定义在function外面,作为全局变量,

奖励园豆:5
jokersora | 初学一级 |园豆:151 | 2017-03-15 20:45

谢谢你。好厉害。

爽7 | 园豆:204 (菜鸟二级) | 2017-03-17 21:14
其他回答(3)
0

楼上说的对。

每一个function就相当于看作为一个盒子,是分开独立的盒子。

如果你想让某一个盒子用到它没有的东西(a),你只能要么就只能从装这些盒子的大盒子拿一个公有的,就是“全局变量”

要么你就只能给这个盒子放进来一个参数,就是“传参”

function clear(a){

clearInterval(a);

} 不过这种你就得在调用的时候传一个a进来

君子如珩~ | 园豆:325 (菜鸟二级) | 2017-03-15 22:29
0

变量a放在Start()方法里是局部变量,在外部是访问不了的,javascript不会报错,但是会找不到对象a,所以你点击停止无效。

        var a;
        function start() {
            a = setInterval(function () {
                document.getElementById("d1").innerText = Math.round((Math.random() * 9));
                document.getElementById("d2").innerText = Math.round((Math.random() * 9));
                document.getElementById("d3").innerText = Math.round((Math.random() * 9));
                document.getElementById("d4").innerText = Math.round((Math.random() * 9));
                document.getElementById("d5").innerText = Math.round((Math.random() * 9));
                document.getElementById("d6").innerText = Math.round((Math.random() * 9));
                document.getElementById("d7").innerText = Math.round((Math.random() * 9));
                document.getElementById("d8").innerText = Math.round((Math.random() * 9));
                document.getElementById("d9").innerText = Math.round((Math.random() * 9));
                document.getElementById("d10").innerText = Math.round((Math.random() * 9));
                document.getElementById("d11").innerText = Math.round((Math.random() * 9));
            }, 100);
        }
        function stop() {
            clearInterval(a);
        }
龙行天涯 | 园豆:1794 (小虾三级) | 2017-03-15 22:32
0

楼上说得对

心怀宇宙 | 园豆:643 (小虾三级) | 2017-03-16 11:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册