setTimeout在几分钟后发疯,FF6.0, Chrome 15
setTimeout goes mad after couple of minutes, FF6.0, Chrome 15
看一下我的news roller: LIVE DEMO
一切都很完美,直到你把脚本留在标签页几分钟。
发生的事情看起来像是浏览器丢失了每5秒的计数,并且不断执行操作。
我猜这和setTimeout
有关。
有什么建议吗?
脚本:
(function($) {
$.fn.NoticeBoard = function() {
// Set a timeout
var timeOut = setTimeout(nextNotice, 5000);
// pause on hover
$('.noticeboard').hover(
function() {
clearTimeout(timeOut);
}, function() {
timeOut = setTimeout(nextNotice, 5000);
});
// Next notice function called on timeout or click
function nextNotice(event) {
clearTimeout(timeOut);
timeOut = setTimeout(nextNotice, 5000);
if ($('.noticeboard span:visible').is('.noticeboard span:last-child')) {
$('.noticeboard span:visible').fadeOut(300);
$('.noticeboard span:first-child').fadeIn();
}
else {
$('.noticeboard span:visible').fadeOut(300).next().fadeIn();
}
return false;
}
$('#notice-next').click(nextNotice);
$('#notice-prev').click(function(event) {
if ($('.noticeboard span:visible').is('.noticeboard span:first-child')) {
$('.noticeboard span:visible').fadeOut(300);
$('.noticeboard span:last-child').fadeIn();
}
else {
$('.noticeboard span:visible').fadeOut(300).prev().fadeIn();
}
return false;
});
};
})(jQuery);
$(document).ready(function() {
$('.noticeboard span').hide();
$('.noticeboard span:first').show();
$('.noticeboard').NoticeBoard();
});