setInterval /clearInterval 范围内的问题
我有以下代码,它工作正常,直到我点击 #play 按钮。我假设这是因为 var intID 设置在另一个地方,并且当我 window.clearInterval() 它时它不在同一范围内...我该如何解决这个问题?顺便说一句,这是 Google 地图 API 版本 3
function intervalTrigger(){
return window.setInterval(function(){
placement++;
if(placement >= markers.length){placement = 0;}
google.maps.event.trigger(markers[placement], "click");
}, 5000);
};
var intID = intervalTrigger();
$('#map_canvas').click(function(){window.clearInterval(intID);});
$('a[href=#nextmarker]').live('click',function(){
placement++;
if(placement >= markers.length){placement = 0};
google.maps.event.trigger(markers[placement], "click");
window.clearInterval(intID);
$('a[href=#pause]').replaceWith('<a href="#play">Play</a>');
return false;
});
$('a[href=#prevmarker]').live('click',function(){
placement--;
if(placement == -1){placement = markers.length-1}
google.maps.event.trigger(markers[placement], "click");
window.clearInterval(intID);
$('a[href=#pause]').replaceWith('<a href="#play">Play</a>');
return false;
});
$('a[href=#play]').live('click',function(){
$('a[href=#play]').replaceWith('<a href="#pause">Pause</a>');
var intID = intervalTrigger();
return false;
});
$('a[href=#pause]').live('click',function(){
window.clearInterval(intID);
$('a[href=#pause]').replaceWith('<a href="#play">Play</a>');
return false;
});
I have the following code, and it works fine until i hit the the #play button. I'm assuming it's because the var intID is set in another place and it's not in the same scope when i window.clearInterval() it... how do I fix this? BTW, this is the Google Maps API Version 3
function intervalTrigger(){
return window.setInterval(function(){
placement++;
if(placement >= markers.length){placement = 0;}
google.maps.event.trigger(markers[placement], "click");
}, 5000);
};
var intID = intervalTrigger();
$('#map_canvas').click(function(){window.clearInterval(intID);});
$('a[href=#nextmarker]').live('click',function(){
placement++;
if(placement >= markers.length){placement = 0};
google.maps.event.trigger(markers[placement], "click");
window.clearInterval(intID);
$('a[href=#pause]').replaceWith('<a href="#play">Play</a>');
return false;
});
$('a[href=#prevmarker]').live('click',function(){
placement--;
if(placement == -1){placement = markers.length-1}
google.maps.event.trigger(markers[placement], "click");
window.clearInterval(intID);
$('a[href=#pause]').replaceWith('<a href="#play">Play</a>');
return false;
});
$('a[href=#play]').live('click',function(){
$('a[href=#play]').replaceWith('<a href="#pause">Pause</a>');
var intID = intervalTrigger();
return false;
});
$('a[href=#pause]').live('click',function(){
window.clearInterval(intID);
$('a[href=#pause]').replaceWith('<a href="#play">Play</a>');
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 #play 点击处理程序中的
var
删除为以下内容:这将正确设置全局
var intID
的值,以便其他事件处理程序可以使用它。Remove the
var
from your #play click handler to the following:That will correctly set the value of the global
var intID
so it's available to the other event handlers.您正在使用该
var
关键字创建一个 new 变量,如果您想在外部作用域中引用该变量,则需要将其取出,如下所示:否则它是只需在
.live()
处理程序中创建一个新变量,该变量不会哪儿也去不了……但是由于您想设置已有的变量,因此请省略var
。You're creating a new variable with that
var
keyword, if you want to reference the variable in the outer scope, you need to take it out, like this:Otherwise it's just creating a new variable inside that
.live()
handler that doesn't go anywhere...but since you want to set the variable you already have, leave off thevar
.