日期:2014-05-16  浏览次数:20370 次

如何让字符一个个停下来?
参考资料写的一个模拟选号,请问如何能点击停止之后,让结果从最后一位向前逐个停止,没有轮到的继续变换数字。
HTML code

<html>
<head>
<title>js模拟选号器</title>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6/jquery.min.js"></script>
<script src="http://lib.sinaapp.com/js/jquery-ui/1.8.9/jquery-ui.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body id="doc">
<div class="yui3-g" style="font-size:24px">
<div class="num yui3-u"></div>

<div class="num yui3-u"></div>

<div class="num yui3-u"></div>

<div class="num yui3-u"></div>

<div class="num yui3-u"></div>

<div class="num yui3-u"></div>

<div class="num yui3-u"></div>
</div>
<script>
$(document).ready(function() {
    var findnumid = $("div").children(".num").length,
    numid,
    Timer,
    timer,
    the_num;
    function random(start, end) {
        var between = end - start + 1;
        return Math.floor(Math.random() * between + start);
    }
    function thenum() {
        the_num = random(0, 9);
        return the_num;
    }
    function time() {
        for (numid = 0; numid < findnumid; numid++) {
            var item = $('div .num')[numid];
            $('body').find(item).html(thenum());
        }

    };
    function display() {
        var mycars = new Array()
        mycars[0] = "q";
        mycars[1] = "w";
        mycars[2] = "e";
        mycars[3] = "i";
        mycars[4] = "k";
        mycars[5] = "j";
        mycars[6] = "b";
        for (numid = 0; numid < findnumid; numid++) {
            var item = $('div .num')[numid];
            $('body').find(item).html(mycars[numid]);
        }
    }
    $("#start").click(function() {
        Timer = setInterval(time, 70);
    });
    $("#stop").click(function() {
        clearInterval(Timer);
        display();
    });
});
</script>
<div id="start">开始</div>
<div id="stop">停止</div>
</body>
</html>



------解决方案--------------------
每个数字变换对应一个定时器比较好控制。
http://jsbin.com/uwutax/3/edit#html,live