我有一个小程序,当您单击“条目”时,将打开编辑模式,而该条目是为其他人锁定的。每隔10秒就会发送一个ajax请求来更新表中的时间戳。
$(".entry-edit").click(function() {
// code
loopLockingVar = setInterval(function() { loopLockingFunction(id) }, 10000);
// code
});
Run Code Online (Sandbox Code Playgroud)
然后,我有一个取消按钮,将表中的时间戳更新为0并清除间隔。
$(".entry-cancel").click(function() {
// code
clearInterval(loopLockingVar);
// code
});
Run Code Online (Sandbox Code Playgroud)
当仅编辑一个条目时,所有操作均有效,但是如果同时处理两个或多个条目,然后单击“取消”,则第一个条目的间隔会更远...
我试过了
var loopLockingVar;
$(".entry-edit").click(function() {
// code
if( ! loopLockingVar) {
loopLockingVar = setInterval(function() { loopLockingFunction(id) }, 10000);
}
// code
});
Run Code Online (Sandbox Code Playgroud)
但是,如果您取消并再次单击“编辑”,此方法将无法正常工作。
我试图在setInterval函数中调用clearInterval,它正在做一些ajax获取,没有任何运气.
var nre = setInterval('checkit()',5000);
$(function() {
checkit = function(){
$.post("check.php", { login: "<?php echo $_SESSION['login'];?>" }, function( data ) {
if (data == 1) {
$('#debug').html(data);
window.clearInterval(nre);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
关键是,循环不会中断,尽管收到正数据.
我已经读过setInterval函数的异步动作可能是这里的问题.有没有更好的方法来解决它?
下面是setInterval()和clearInterval()... 的脚本
我已经搜索过clearInterval()要停止setInterval()...我实现了那段代码,但它无法正常工作..
我想在这里实现的是,例如,有8个捕食者和3个猎物..当捕食者多于猎物时,setInterval将开始,因此猎物将减少3,而捕食者将增加3个.当猎物的数量达到0时.捕食者应该停止增加,因为没有更多的猎物离开..
这里的问题是,当猎物达到0时,捕食者仍然在增加..我加入clearInterval()但它不起作用..我可以知道出了什么问题吗?
<script>
function startAni() {
if ((document.getElementById('amount').value) >= (document.getElementById('amount5').value)) {
alert("Predator is more than prey! FRENZY TIME!");
var interval = setInterval(function () {
Remove3(), removevalue3(), Add(), addvalues();
if (document.getElementById('amount5').value = "0") {
alert("No more Prey!");
clearInterval(interval);
}
}, 5000);
} else {
alert("Prey is more than Predator! Revenge time!")
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!
我使用以下代码隐藏并每1秒显示一个div
$(document).ready(function(e) {
var acc = setInterval(function(){
$("#element").fadeOut(function(){
setTimeout(function(){
$("#element").fadeIn()
}, 1000)
})
}, 200)
})
$(window).load(function(){
setTimeout(function(){
clearInterval(acc);
}, 10000)
})
Run Code Online (Sandbox Code Playgroud)
在clearInterval()不工作,因为它应该和我得到谷歌Chrome以下错误:
未捕获的ReferenceError:未定义acc
我创建了一个自动滑动的旋转木马每5秒钟(以及手动按钮移动幻灯片).我希望能够在鼠标悬停在幻灯片上时暂停滚动,并在鼠标不再悬停时恢复.
到目前为止,我的脚本将执行以上所有操作.
但是它不会在页面加载时启动,您必须将鼠标悬停在然后取消悬停才能启动.
这是我的脚本:
jQuery('#viewport').hover(function () {
window.clearInterval(timer);
}, function () {
timer = window.setInterval(function () {
jQuery('#next').trigger('click');
}, 1000);
});
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让它立即开始滑动?
function iPadMovie(id) {
$(function () {
var i = 1;
var interval = setInterval(function () {
jQuery('.animationMax img').attr({
src: 'http://jdsports.scene7.com/is/image/JDSports/127932jd' + ('0' + i).slice(-2) + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
});
i++;
if (i === 28) i = 1;
}, 100);
});
}
function playIpad(){
iPadMovie();
}
function stopIpad(){
clearInterval = interval;
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到小提琴:http://jsfiddle.net/Vv2u3/15/我希望能够停止电影并重新启动它,如果他们按下播放.当然我可以在方法之外使用clearInterval吗?
很简单,setIntervall 通常以 't' 开头,go 为 false,clearinterval 什么也不做,go 为 true。所以如果 app.post 第三次执行,我有 2 个间隔,处理 t。我不明白为什么。
go= true;
app.post('/s', function s(req, res){
if (go){
setInterval(t, 3000);
go = false;
}else{
clearInterval(t);
go = true;
}
});
Run Code Online (Sandbox Code Playgroud) setInterval() 对我来说工作正常并且计时器启动,但是当计数器值达到 100 时 clearInterval() 不会停止计时器。它连续运行。任何帮助表示赞赏。
以下是我的组件代码 -
export class AppComponent {
counter=0;
progressInterval;
ngOnInit(){
this.progressInterval=setInterval(()=>{
this.counter=this.counter+10;
if(this.counter>=100){
clearInterval(this.progressInterval);
}
},200);
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我的组件 HTML 代码 -
<p style="margin:20px;">
<ngb-progressbar
type="warning"
[value]="counter"
[striped]="true"
[animated]="true"
>{{counter}}</ngb-progressbar>
</p>
Run Code Online (Sandbox Code Playgroud)
这是显示进度条的屏幕截图 -
谢谢
我有这段代码:
class CombatLog {
constructor(){
this.idMsg = 0;
this.timerInterval;
}
startTimer(){
this.timerInterval = setInterval(this.combatStartLog, 2000);
$('#combatLog').empty();
}
combatStartLog(){
console.log(this.idMsg);
switch (this.idMsg){
case 3:
clearInterval(this.timerInterval);
$('#combatLog').empty();
break;
case 2:
$('<p>', {
class: 'combatText',
id: `${this.idMsg}`
}).appendTo('#combatLog');
$(`#${this.idMsg}`).append(`FIGHT!`);
this.idMsg = 3;
break;
case 1:
$('<p>', {
class: 'combatText',
id: `${this.idMsg}`
}).appendTo('#combatLog');
$(`#${this.idMsg}`).append(`Prepare your potions...`);
this.idMsg = 2;
break;
case 0:
$('<p>', {
class: 'combatText',
id: `${this.idMsg}`
}).appendTo('#combatLog');
$(`#${this.idMsg}`).append(`Unsheathe your weapons...`);
this.idMsg = 1;
break;
default:
this.idMsg = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
所需的行为将是:
鼠标悬停时循环开始很好,但鼠标悬停时并没有停止!我究竟做错了什么?
脚本
<script type="text/javascript">
function iniciarep(x) {
var iddointervalo = setInterval(function(){ change(x); }, 500);
}
function terminarep() {
clearInterval(iddointervalo);
}
function change(x) {
**do stuff here (working fine)**
}
</script>
Run Code Online (Sandbox Code Playgroud)
的HTML
<img id="c3" src="letras/m1.svg" alt="m" onmouseover='iniciarep(this);' onmouseout='terminarep();'>
Run Code Online (Sandbox Code Playgroud) clearinterval ×10
javascript ×8
setinterval ×7
jquery ×4
ajax ×1
angular ×1
angular5 ×1
carousel ×1
constructor ×1
function ×1
hover ×1
html ×1
post ×1
settimeout ×1