我正在尝试在暂停时启动setInterval
循环/在mouseout上停止它。鼠标输出有效,但循环最初不仅在悬停时开始。
// Loop trough a set of images
var loop = setInterval(function(){
rotator.src = dir + num+'.jpg';
num = (num === len) ? 0 : ++num;
}, delayInMilliseconds);
// The loop should only start on hover
$( '#rotator' ).hover(
function() {
console.log( 'hover' );
},
// The loop should stop on mouseout
function() {
clearInterval( loop );
console.log( 'no hover' );
}
)
答案 0 :(得分:0)
var interval = null;
function animatedCode() {
rotator.src = dir + num+'.jpg';
num = (num === len) ? 0 : ++num;}
//Stop and start on hover
$('#rotator').hover(function() {
interval = window.setInterval(function(){animatedCode()},delayInMilliseconds);
}, function() {
window.clearInterval(interval);
});