简体   繁体   English

setInterval 不适用于 onmouseover

[英]setInterval doesn’t work with onmouseover

I'm using luxon to get some different timezones' information.我正在使用 luxon 获取一些不同时区的信息。

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.当鼠标悬停在内容 div 上时,它必须显示城市的当前时间,当调用 onmouseout 时,再次显示城市名称。

But somehow setInterval doesn't work with onmouseover function, just it stays with stopped time information.但不知何故 setInterval 不适用于 onmouseover function,它只保留停止时间信息。

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>

This calls updateTimes upon execution and sets what it returns as its callback function .这会在执行时调用updateTimes并将返回的内容设置为callback function

const time = setInterval(updateTimes(), 1000);

This is the first error.这是第一个错误。 You want to pass it like this:你想像这样传递它:

const time = setInterval(updateTimes, 1000);

So you don't execute the function and pass what it returns, but pass the function itself.所以你不执行 function 并传递它返回的内容,而是传递 function 本身。

clearInterval doesn't take a function but an interval id. clearInterval不采用 function,而是采用间隔 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: setInterval返回该 id 以便清除间隔,您必须保存间隔 id 并将其用作第一个参数,如下所示:

const time = setInterval(updateTimes, 1000);
clearInterval(time);

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.那么让我们看看 updateTimes function 并看看它做了什么。 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:这是我的解决方案:

 const one = document.querySelector(".div1 div div"); const timezone = one.getAttribute("data-timezone"); let now = luxon.DateTime.now().setZone(timezone); const city = one.getAttribute("data-city"); let interval = null; // we set the interval here if it is already set we clear the previous interval // mouseenter is only called once when we enter mouseover gets called everytime we move our mouse and it is inside the element one.addEventListener("mouseenter", () => { if (interval) clearInterval(interval); // we save the interval id to clear it later interval = setInterval(() => { // Here we update the time and text now = luxon.DateTime.now().setZone(timezone); one.innerHTML = now.toFormat("LLL dd HH:mm:ss"); }, 1000); // we do it again once so we don't have to wait for one second the first time now = luxon.DateTime.now().setZone(timezone); one.innerHTML = now.toFormat("LLL dd HH:mm:ss"); }); // Here we clear the interval // mouseleave so we only ever clear when the mouse moves out of the element one.addEventListener("mouseleave", () => { // when we leave and the inteval is set we clear it if (interval) { clearInterval(interval); interval = null; } // and reset the text 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"> <!-- There was a typo in data-timezone="America/New_Yrok" --> <div data-city="New York" data-timezone="America/New_York">New York</div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM