JS里setInterval和clearInterval的小问题,请教下,谢谢啦

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>
<div id="we" style="width:100px; height:100px; position:absolute; top:0; left:0; background:red;"></div>
<br/><br/><br/><br/><br/><br/><br/>
<input type="button" value="点击A" onclick="showA()"/>
<input type="button" value="点击B" onclick="showB()"/>
</body>
<script type="text/javascript">
    var a=0;
function move(){
    a=a+30;
    document.getElementById('we').style.left=a+'px';
    }
function showA(){
    timer=window.setInterval('move()',200);
    }   
function showB(){
    window.clearInterval(timer);
    }

</script>
</html>

timer=setInterva在showA()这个函数里,
window.clearInterval在showB这个函数里,
点击showA(),div盒子开始滑动,
为什么点击showB可以令showA里的timer停止?

不用var申明的变量都是window作用域下的,所以你的showB可以引用到变量timer变量,如果是下面那样就没办法引用到了,只能在showA里面使用

 function showA(){
  var  timer=window.setInterval('move()',200);
    }

是的,timer=window.setInterval('move()',200);这返回了定时器的局部timer,可以通过这个返回关闭定时器,也就是showb的作用。
这里timer是页面全局变量。

为什么timer是全局产量呢?