To be or not to be.That is a question!

---源于莎士比亚的《哈姆雷特》

导航

Using Function Closures with Timers

<!DOCTYPE HTML>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            #redbox{position: absolute;left: 100px;top: 100px;width: 200px;height: 200px;background-color: red;}
        </style>
    </head>
    <body>
        <script type="text/javascript">
            var intervalId=null;
            window.onload=function(){
                document.getElementById("redbox").onclick=stopStartElement;
            }
            function stopStartElement(){
                if(intervalId==null){
                    var x=100;
                    intervalId=setInterval(function(){
                        x+=5;
                        var left=x+"px";
                        document.getElementById("redbox").style.left=left;
                    },100);
                }else{
                    clearInterval(intervalId);
                    intervalId=null;
                }
            }
        </script>
        <div id="redbox"></div>
    </body>
</html>

 

posted on 2013-05-27 21:43  Ijavascript  阅读(148)  评论(0编辑  收藏  举报