为什么javascript向setInterval id添加1?
在mouseout事件激发后,我与添加的1有效;例如,在悬停时,它会向12059发出警报,但在鼠标离开时会向12060发出警报为什么javascript向setInterval id添加1?,javascript,jquery,Javascript,Jquery,在mouseout事件激发后,我与添加的1有效;例如,在悬停时,它会向12059发出警报,但在鼠标离开时会向12060发出警报 谢谢 问题是您使用的是“悬停”而不是“鼠标悬停”。 鼠标悬停似乎同时在鼠标悬停和鼠标悬停上触发: $('#interviehover').hover(function(){ IntervalId = setInterval(sec, 500) alert(IntervalId) }); $(
谢谢 问题是您使用的是“悬停”而不是“鼠标悬停”。 鼠标悬停似乎同时在鼠标悬停和鼠标悬停上触发:
$('#interviehover').hover(function(){
IntervalId = setInterval(sec, 500)
alert(IntervalId)
});
$('#interviehover').mouseout(function(){
clearInterval(IntervalId)
alert(IntervalId )
});
hover的签名包含两个函数,一个用于mouseenter,一个用于mouseleave:
.hover(handlerIn(eventObject)、handlerOut(eventObject))
问题是您使用的是“hover”而不是“mouseover”。
鼠标悬停似乎同时在鼠标悬停和鼠标悬停上触发:
$('#interviehover').hover(function(){
IntervalId = setInterval(sec, 500)
alert(IntervalId)
});
$('#interviehover').mouseout(function(){
clearInterval(IntervalId)
alert(IntervalId )
});
hover的签名包含两个函数,一个用于mouseenter,一个用于mouseleave:
.hover(handlerIn(eventObject)、handleout(eventObject))
在您的示例中,setInterval
可能被多次调用,因此IntervalId
的值被覆盖
不要将alert
与hover
/mouseout
结合使用,尤其是在调试时。出现的警报窗口将导致意外触发这些事件
不要使用这样可以覆盖的全局变量,或者可以多次执行setInterval
,并且只能清除最后一个
hover()
接受两个参数,一个用于mouseover
事件,一个用于mouseout
事件。不要使用hover()
并设置mouseover
和mouseout
,或者不要使用mouseout
并使用hover
的两个参数,在您的示例中setintervalid
的值可能会被多次调用,从而覆盖IntervalId
的值
不要将alert
与hover
/mouseout
结合使用,尤其是在调试时。出现的警报窗口将导致意外触发这些事件
不要使用这样可以覆盖的全局变量,或者可以多次执行setInterval
,并且只能清除最后一个
hover()
接受两个参数,一个用于mouseover
事件,一个用于mouseout
事件。不要使用hover()
并设置mouseover
和mouseout
,或者不要使用mouseout
并同时使用hover
的两个参数,因为当只传递一个函数时,悬停函数会运行两次,因此您会看到错误
您可以将其更改为接受两个参数,然后它将停止您看到的内容:
$('#interviehover').mouseover(function() {
IntervalId = setInterval(sec, 500);
alert("hover:"+IntervalId);
});
$('#interviehover').mouseout(function() {
clearInterval(IntervalId);
alert("mouseout:"+IntervalId);
});
var有效期;
$('#interviehover').hover(函数(){
IntervalId=setInterval(第500秒)
$('#result').append('以ID开头:'+IntervalId+'
');
},函数(){
clearInterval(有效期间)
$('#result').append('Cleared ID:'+IntervalId+'
');
});
函数sec(){
$('#result').append('Running with ID:'+IntervalId+'
');
}
查看上面的您看到了错误,因为当您只传递一个函数时,您的悬停函数会运行两次 您可以将其更改为接受两个参数,然后它将停止您看到的内容:
$('#interviehover').mouseover(function() {
IntervalId = setInterval(sec, 500);
alert("hover:"+IntervalId);
});
$('#interviehover').mouseout(function() {
clearInterval(IntervalId);
alert("mouseout:"+IntervalId);
});
var有效期;
$('#interviehover').hover(函数(){
IntervalId=setInterval(第500秒)
$('#result').append('以ID开头:'+IntervalId+'
');
},函数(){
clearInterval(有效期间)
$('#result').append('Cleared ID:'+IntervalId+'
');
});
函数sec(){
$('#result').append('Running with ID:'+IntervalId+'
');
}
查看上面的,正如您在查看jquery悬停方法时所看到的:
var IntervalId;
$('#interviehover').hover(function() {
IntervalId = setInterval(sec, 500)
$('#result').append('Starting with ID: '+ IntervalId +'<br/>');
}, function() {
clearInterval(IntervalId)
$('#result').append('Cleared ID: '+ IntervalId +'<br/>');
});
function sec() {
$('#result').append('Running with ID: '+ IntervalId +'<br/>');
}
如果只提供了1个函数,则它将其用于输入和离开。
当您输入时,它会设置一个计时器。然后当你离开时,它会再次设置一个新的计时器。
第二个计时器ID可能是第一次+1
尽管它被指定为“唯一”,但很有可能它只是作为增量实现的(因为这也是唯一的)当您查看jquery悬停方法时可以看到:
var IntervalId;
$('#interviehover').hover(function() {
IntervalId = setInterval(sec, 500)
$('#result').append('Starting with ID: '+ IntervalId +'<br/>');
}, function() {
clearInterval(IntervalId)
$('#result').append('Cleared ID: '+ IntervalId +'<br/>');
});
function sec() {
$('#result').append('Running with ID: '+ IntervalId +'<br/>');
}
如果只提供了1个函数,则它将其用于输入和离开。
当您输入时,它会设置一个计时器。然后当你离开时,它会再次设置一个新的计时器。
第二个计时器ID可能是第一次+1
虽然它被指定为“唯一”,但很有可能它只是作为增量实现的(因为这也是唯一的)您可能需要重新表述您的问题,因为您不太清楚您的目的。我的意思是-相同的IntervalId具有不同的值您可能需要重新表述您的问题,因为不太清楚你在追求什么。我的意思是-同一个IntervalId有不同的值