看不到 JS 中的问题
我希望当鼠标悬停在图像上时,应该触发一次事件,并且只有在鼠标离开该图像并再次返回后,并且至少经过 2 秒后,才应该再次触发该事件。
如果我将鼠标悬停在图像上,它会像每毫秒一样被调用,并且根据我的函数逻辑,一旦您将鼠标悬停在变量“canhover”上,它就会变为 0,直到您将鼠标移出
此代码似乎有一个错误,我看不到它。我需要一双新的眼睛,但算法有点合乎逻辑
工作代码:
<script type="text/javascript">
var timeok = 1;
function redotimeok() {
timeok = 1;
}
//
function onmenter()
{
if (timeok == 1)
{
enter();
timeok = 0;
}
}
//
function onmleave()
{
setTimeout(redotimeok, 2000);
leave();
}
//
$('#cashrefresh').hover(onmenter,onmleave);
function enter(){
$("#showname").load('./includes/do_name.inc.php');
$("#cashrefresh").attr("src","images/reficonani.gif");
}
function leave(){
$("#cashrefresh").attr("src","images/reficon.png");
}
</script>
I want that when mouse is over an image, an event should be triggered ONCE, and it should be triggered again only after mouse is out of that image and back again, and also at least 2 seconds passed.
If I leave my mouse over the image,it gets called like every milisecond,and by the logic of my function once you hover on the variable 'canhover' becomes 0 until you move mouse out
This code seems to have a bug and I cant see it. I need a new pair of eyes, but the algorithm is kinda logical
Working code :
<script type="text/javascript">
var timeok = 1;
function redotimeok() {
timeok = 1;
}
//
function onmenter()
{
if (timeok == 1)
{
enter();
timeok = 0;
}
}
//
function onmleave()
{
setTimeout(redotimeok, 2000);
leave();
}
//
$('#cashrefresh').hover(onmenter,onmleave);
function enter(){
$("#showname").load('./includes/do_name.inc.php');
$("#cashrefresh").attr("src","images/reficonani.gif");
}
function leave(){
$("#cashrefresh").attr("src","images/reficon.png");
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道这是否能解决您的整个问题(因为我们没有详细说明它是什么),而是:
尝试:
这里也是同样的事情:
另外,我不知道您曾经在哪里将
timeok
设置为零。您的意思是在onmenter()
中设置timeok = 0
吗?I don't know if this will solve your entire problem (since we don't have a detailed description of what it is), but instead of:
try:
And the same thing here:
Also, I don't see where you ever set
timeok
to zero. Do you mean to settimeok = 0
inonmenter()
?jquery 有两种方法可以解决您的问题:
.mouseenter()
和.mouseleave()
查看那里的演示。
编辑:
我认为
hover
适用于mouseover
和mouseout
,抱歉造成混淆。我又检查了你的代码。当鼠标悬停在图像上时,您似乎正在更改图像,这会强制浏览器加载新图像,并且旧图像会消失一小会儿,直到新图像出现,我认为这必须连续触发两个处理程序你就会得到这种行为。
尽量不要更改图像的来源,注释掉该行,然后使用
console.log("some message")
并查看该消息是否与.load() 一样重复
之前被解雇过。希望这有帮助。
There are two methods in jquery for your problem:
.mouseenter()
and.mouseleave()
Check out the demos there.
EDIT:
I thought
hover
was formouseover
andmouseout
, sorry for confusion.I checked your code again. And it seems that you're changing the image when mouse gets over the image, which forces browser to load the new image and the old image disappears for a very little while till the new one appears and i think this must be triggering both handlers continuosly and you're getting this behaviour.
Try not to change the source of the image, comment out that line and instead
console.log("some message")
there and see if the message is repeated as much as.load()
was fired before.Hope this helps.
尝试更改 onmleave 函数,如下所示:
Try changing onmleave function as follows: