清除setInterval函数后如何恢复?请只使用javascript
How to resue setInterval function after clearing it? Only javascript please
Onmouseover(在图像上(我正在清除setInterval函数。在mouseout中,我需要重用相同的setInterval函数。但由于它在mouseover事件中被清除,因此在mouseout上不起作用。如何在mouseout上再次使用setinterval函数?
我正在尝试创建一个幻灯片小部件(类似于YAHOO主页(。
p = 0;
function list(event) {
var x = event.target || event.srcElement;
document.getElementById("outer").src = x.src;
x.style.borderBottom = "thick solid #0000FF";
allimg[0].getElementsByTagName('img')[g].style.borderBottom = "none";
clearInterval(cha); //clearing autoincrement of images onmouseover
clearInterval(rcli); //clearing autoright click button onmouseover
clearInterval(chanid); //??? CLEARING HERE
x.addEventListener('mouseout', function () {
x.style.borderBottom = "none";
function chan() {
allimg = document.getElementsByClassName("container"); //container within which all images are there
u = allimg[0].getElementsByTagName('img');
for (p; p < u.length; p++) {
if (u[p].style.borderBottomStyle == "solid" || u[p] == x) {
a = p;
u[a].style.borderBottom = "none";
a++;
document.getElementById("outer").src = u[a].src;
u[a].style.borderBottom = "thick solid #0000FF";
break;
}
}
p++;
}
chanid = setInterval(chan, 2000);
});
}
在函数中创建间隔,这样在mouseout中就可以重新运行此函数。
另一种方法是保持间隔运行,但更新一个变量,使间隔函数无法完全执行。类似这样的东西:
var paused;
setInterval(onInterval, 1000);
$img.on("mouseover", function () {
// on mouseover 'pause' the interval
paused = true;
})
.on("mouseout", function () {
// on mouseout 'unpause' it
paused = false;
});
function onInterval () {
// if paused do nothing
if(paused) return;
// do regular interval stuff here
}
相关文章:
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- javascript恢复样式
- 在文本区域禁用javascript定义的keydown事件并恢复默认行为
- 如何从本地存储恢复javascript值
- 如何在 v8 中正确恢复 Javascript 上下文
- 如何在恢复函数之前等待 JavaScript 承诺解析
- Javascript:保存和恢复浏览器窗口大小
- Javascript:保存和恢复样式状态在IE中不起作用
- Javascript:onclick函数工作一次,页面就会恢复到原来的状态
- 在IE8中恢复被覆盖的javascript方法
- 在没有F12工具的情况下恢复Internet Explorer上的javascript日志和错误
- 如何通过在javascript中单击浏览器的后退按钮将页面恢复到以前的状态
- 恢复javascript函数中的href属性
- 如何在Visual Studio代码中恢复Javascript语法错误检测
- Javascript:如何清除文本框并恢复其值
- 在浏览器返回/forwd 按钮单击时保存/恢复所有 JavaScript 变量
- 使用 JavaScript 恢复滚动事件不起作用
- JavaScript:恢复为同步的异步函数
- 不使用JavaScript恢复web应用程序的进度
- 清除文本输入的值,如果复选框被选中,使用javascript恢复值,如果未选中