为什么timer不能再onload事件里声明呢
<div id="item" class="main"><span id = "spare" class = "sp">分享</span></div>
<script type="text/javascript">
window.onload = function (){
var item=document.getElementById("item");
item.onmouseover = function(){
startMove(0);
}
item.onmouseout = function(){
startMove(-400);
}
}
var timer = null;
function startMove(target){
var item=document.getElementById("item");
clearInterval(timer);
timer = setInterval (function(){
speed = (item.offsetLeft > target)?-10 : 10;
if (item.offsetLeft == target){
clearInterval(timer);
}
else{
item.style.left = item.offsetLeft + speed + "px";
}
},30)
}
</script>