setInterval不使用onmouseover
我正在使用Luxon来获取一些不同的时区信息。
当鼠标在内容div上结束时,它必须显示一个城市的当前时间,当称为“笨拙”时,请再次显示城市的名字。
但是以某种方式SetInterval与Onmouseover功能不起作用,只是与停止的时间信息保持在一起。
我要制作的是实时时钟,当我在每个城市名称上鼠标鼠标时可以运行。我该如何使其工作?
代码样本:
const one = document.querySelector(".div1 div div");
const timezone = one.getAttribute("data-timezone");
const now = luxon.DateTime.now().setZone(timezone);
const city = one.getAttribute("data-city");
const time = setInterval(updateTimes(), 1000);
function updateTimes() {
one.onmouseover = function() {
one.innerHTML = now.toFormat("LLL dd HH:mm:ss");
};
}
const stoptime = clearInterval(stopTimes(), 1000);
function stopTimes() {
one.onmouseout = function() {
one.innerHTML = city;
};
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.2/luxon.min.js"></script>
<div class="div1 content">
<div class="wrap">
<div data-city="New York" data-timezone="America/New_Yrok">New York</div>
</div>
</div>
I'm using luxon to get some different timezones' information.
When mouse is over on a content div, it has to show the present time of a city and when onmouseout is called, show the city's name again.
But somehow setInterval doesn’t work with onmouseover function, just it stays with stopped time information.
What I want to make is real time clocks that can run when I mouse over on each city name. How can I make it work?
Code Sample:
const one = document.querySelector(".div1 div div");
const timezone = one.getAttribute("data-timezone");
const now = luxon.DateTime.now().setZone(timezone);
const city = one.getAttribute("data-city");
const time = setInterval(updateTimes(), 1000);
function updateTimes() {
one.onmouseover = function() {
one.innerHTML = now.toFormat("LLL dd HH:mm:ss");
};
}
const stoptime = clearInterval(stopTimes(), 1000);
function stopTimes() {
one.onmouseout = function() {
one.innerHTML = city;
};
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.2/luxon.min.js"></script>
<div class="div1 content">
<div class="wrap">
<div data-city="New York" data-timezone="America/New_Yrok">New York</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将在执行后调用
updateTimes
,并将其返回的内容设置为其回调函数
。这是第一个错误。您想这样传递它:
因此,您不会执行功能并传递返回的功能,而要传递函数本身。
clear Interval
不使用函数,而是间隔ID。setInterval
返回该ID,因此要清除您必须保存间隔ID的间隔并将其用作第一个参数,也只有一个输入参数。
但是您不想立即清除间隔,因为它根本不会被调用。
因此,让我们看一下UpdateTimes功能,看看它的作用。它为Mouseover事件设置了一个新事件侦听器。这应该起作用,但是您应该尝试将逻辑保留在事件侦听器中。
这是我的解决方案:
This calls
updateTimes
upon execution and sets what it returns as itscallback function
.This is the first error. You want to pass it like this:
So you don't execute the function and pass what it returns, but pass the function itself.
clearInterval
doesn't take a function but an interval id.setInterval
returns that id so to clear the interval you have to save the interval id and use that as the first argument like so:It also only has one input parameter.
But you don't want to clear the interval right away because then it wouldn't be called at all.
So let's look at the updateTimes function and see what it does. It sets a new event listener for the mouseover event. This should work but instead you should try to keep the logic in the event listener.
This is my solution: