如何在鼠标悬停在元素上时暂停计时器(Javascript/JQuery)

huangapple go评论59阅读模式
英文:

How to pause timer on mouse over element (Javascript/JQuery)

问题

我尝试查看其他答案,但没有运气。

我有一个每隔10秒刷新一次的iFrame,但如果用户此刻正在使用iFrame,这会让人感到沮丧。因此,我希望当鼠标悬停在iFrame上时,iFrame不会被刷新。

function iframe() {
  $("#iframe").attr("src","admin.php"); 
}
var timee = 10000;

var intervalId = window.setInterval(function(){
  iframe();
}, timee);

任何帮助都将不胜感激。

英文:

I tried looking at other answers but with no luck.

I have iFrame that refreshes every 10 seconds, but it is frustrating if user is using iFrame at the moment.
So I want to make it when there is mouse cursos over iframe, that iframe won't be refreshed.

function iframe() {
  $("#iframe").attr("src","admin.php"); 
}
var timee = 10000;

var intervalId = window.setInterval(function(){
  iframe();
}, timee);

Any help is appreciated.

答案1

得分: 1

你可以使用onmouseenteronmouseout事件来监听鼠标是否位于框架上方,清除间隔并在鼠标再次离开时重新启动它。

它不一定会“暂停”刷新,而是会中止并在鼠标离开时重新启动间隔,但这比尝试暂停要容易得多。

以下是一个示例,每秒更改背景颜色,除非鼠标悬停在上面。

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-js -->

const timee = 1000;

const iframe = document.getElementById("iframe");
iframe.addEventListener("mouseenter", pause);
iframe.addEventListener("mouseout", unpause);

let intervalId;

function pause() {
  window.clearInterval(intervalId);
}

function unpause() {
  intervalId = window.setInterval(function() {
    iframeUpdate();
  }, timee);
}

// 在第一次运行时启动间隔
unpause();

function iframeUpdate() {
  iframe.style.backgroundColor = `hsl(${Math.random() * 360}, 80%, 50%)`;
}

<!-- 语言: lang-css -->

#iframe {
  width: 100px;
  height: 100px;
  border: 1px solid black;
}

<!-- 语言: lang-html -->

<iframe id="iframe"></iframe>

<!-- 结束代码片段 -->
英文:

You can use onmouseenter and onmouseout events to listen to the mouse being above the frame, clear the interval and restart it once the mouse leaves again.

It doesn't necessarily "pause" the refresh, but instead aborts it and restarts the interval once the mouse leaves, but that's a lot easier than trying to pause it.

Here is an example that changes the background color every second, unless hovered over.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const timee = 1000;

const iframe = document.getElementById(&quot;iframe&quot;);
iframe.addEventListener(&quot;mouseenter&quot;, pause);
iframe.addEventListener(&quot;mouseout&quot;, unpause);

let intervalId;

function pause() {
  window.clearInterval(intervalId);
}

function unpause() {
  intervalId = window.setInterval(function() {
    iframeUpdate();
  }, timee);
}

// start the interval on the first run
unpause();

function iframeUpdate() {
  iframe.style.backgroundColor = `hsl(${Math.random() * 360}, 80%, 50%)`;
}

<!-- language: lang-css -->

#iframe {
  width: 100px;
  height: 100px;
  border: 1px solid black;
}

<!-- language: lang-html -->

&lt;iframe id=&quot;iframe&quot;&gt;&lt;/iframe&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 19:06:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定