如何停止javascript中的嵌套函数?
javaScript新手。我来自java,学习js。因此,我很难找到如何在js中应用阻止函数运行的条件。我应该使用布尔值吗?喜欢真假条件吗?如果是,你怎么做如何停止javascript中的嵌套函数?,javascript,jquery,Javascript,Jquery,javaScript新手。我来自java,学习js。因此,我很难找到如何在js中应用阻止函数运行的条件。我应该使用布尔值吗?喜欢真假条件吗?如果是,你怎么做 <div class="upper"> <div class="aniShell"> <div class="aniOne ab"><img class="photo" src="<?php echo $img ?>"> </div>
<div class="upper">
<div class="aniShell">
<div class="aniOne ab"><img class="photo" src="<?php echo $img ?>"> </div>
<div class="aniTwo ab"><img class="photo"src="<?php echo $img2 ?>"> </div> <!--coming from includes/lastThreeOfBigHit-->
<div class="aniThree ab"><img class="photo" src="<?php echo $img3 ?>"> </div>
<div class='dotContainer'>
<div class="dot"id="dot1"></div>
<div class="dot"id="dot2"></div>
<div class="dot"id="dot3"></div>
</div>
<div class="aniTitle a1"><?php echo ucwords($title) ?><!--<br> <p><?php echo $desc ?></p>--></div>
<div class="aniTitle a2"><?php echo ucwords($title2) ?><!--<br><p><?php echo $desc2 ?></p>--></div>
<div class="aniTitle a3"><?php echo ucwords($title3) ?><!--<br> <p><?php echo $desc3 ?></p>--></div>
</div>
var x=0;
function animate(){
if(x==0){
$(".aniTwo, .a2").hide(); $(".aniThree, .a3").hide();
$("#dot1").addClass("black");
$("#dot1").addClass("black");
function infinite(){
setTimeout(function(){
$(".aniOne, .a1").fadeOut(); $(".aniTwo, .a2").fadeIn();
$("#dot1").removeClass("black");
$("#dot2").addClass("black");
}, 3000);
setTimeout(function(){
$(".aniTwo, .a2").fadeOut(); $(".aniThree, .a3").fadeIn();
$("#dot2").removeClass("black");
$("#dot3").addClass("black");
}, 6000);
setTimeout(function(){
$(".aniThree, .a3").fadeOut(); $(".aniOne, .a1").fadeIn();
$("#dot3").removeClass("black");
$("#dot1").addClass("black");
infinite();
},9000);
}
infinite();
}else{alert("hi")}
$("#dot1").click(function(){
x=1; //ISN'T IT SHOULD STOP FUNCTION RUNNING?
});
}
">
">
">
var x=0;
函数animate(){
如果(x==0){
$(.aniTwo.a2”).hide();$(.aniTwo.a3”).hide();
$(“#dot1”).addClass(“黑色”);
$(“#dot1”).addClass(“黑色”);
函数无穷(){
setTimeout(函数(){
$(“.anionee.a1”).fadeOut();$(.aniTwo.a2”).fadeIn();
$(“#dot1”).removeClass(“黑色”);
$(“#dot2”).addClass(“黑色”);
}, 3000);
setTimeout(函数(){
$(.aniTwo.a2”).fadeOut();$(.aniTwo.a3”).fadeIn();
$(“#dot2”).removeClass(“黑色”);
$(“#dot3”).addClass(“黑色”);
}, 6000);
setTimeout(函数(){
$(.aniThree,.a3”).fadeOut();$(.negation,.a1”).fadeIn();
$(“#dot3”).removeClass(“黑色”);
$(“#dot1”).addClass(“黑色”);
无限();
},9000);
}
无限();
}else{alert(“hi”)}
$(“#dot1”)。单击(函数(){
x=1;//它不应该停止函数运行吗?
});
}
要停止所有绑定到setTimeout()
的函数运行,需要使用方法清除所有计时器
您需要在
infinite()函数中移动if(x==0)
条件:
var x=0;
function animate(){
// if(x==0){ // <-- FROM HERE
$(".aniTwo, .a2").hide(); $(".aniThree, .a3").hide();
$("#dot1").addClass("black");
$("#dot1").addClass("black");
function infinite(){
if (x==0){ // <-- TO HERE
setTimeout(function(){
$(".aniOne, .a1").fadeOut(); $(".aniTwo, .a2").fadeIn();
$("#dot1").removeClass("black");
$("#dot2").addClass("black");
}, 3000);
setTimeout(function(){
$(".aniTwo, .a2").fadeOut(); $(".aniThree, .a3").fadeIn();
$("#dot2").removeClass("black");
$("#dot3").addClass("black");
}, 6000);
setTimeout(function(){
$(".aniThree, .a3").fadeOut(); $(".aniOne, .a1").fadeIn();
$("#dot3").removeClass("black");
$("#dot1").addClass("black");
infinite();
},9000);
}
//infinite(); // <-- move down
}
else{
alert("hi")
}
infinite(); // <-- to here
$("#dot1").click(function(){
x=1; //ISN'T IT SHOULD STOP FUNCTION RUNNING?
});
}
var x=0;
函数animate(){
//if(x==0){/作为一个快速修复,我认为您也可以在infinite
函数中添加if(x==0)
条件
对函数稍作修改可以使其变得更小
var x = 0,
currState = 0,
stateData = [
['.aniOne, .a1', '#dot1'],
['.aniTwo, .a2', '#dot2'],
['.aniThree, .a3', '#dot3']
];
function animate() {
if (x == 0) {
$("#dot1").click(function() {
x = 1;
});
$(".aniTwo, .a2").hide();
$(".aniThree, .a3").hide();
$("#dot1").addClass("black");
$("#dot1").addClass("black");
infinite();
}
}
function infinite() {
if (x == 0) {
setTimeout(function() {
var nextState = (currState + 1) % 3;
$(stateData[currState][0]).fadeOut();
$(stateData[nextState][0]).fadeIn();
$(stateData[currState][1]).removeClass("black");
$(stateData[nextState][1]).addClass("black");
currState = nextState;
infinite();
}, 3000);
} else {
alert("hi");
}
}
返回false或true;将此语句写在要停止执行的位置GoogleclearTimeout
。这是setTimeout
经常被忽略的近亲。能否添加您正在使用的html
var x = 0,
currState = 0,
stateData = [
['.aniOne, .a1', '#dot1'],
['.aniTwo, .a2', '#dot2'],
['.aniThree, .a3', '#dot3']
];
function animate() {
if (x == 0) {
$("#dot1").click(function() {
x = 1;
});
$(".aniTwo, .a2").hide();
$(".aniThree, .a3").hide();
$("#dot1").addClass("black");
$("#dot1").addClass("black");
infinite();
}
}
function infinite() {
if (x == 0) {
setTimeout(function() {
var nextState = (currState + 1) % 3;
$(stateData[currState][0]).fadeOut();
$(stateData[nextState][0]).fadeIn();
$(stateData[currState][1]).removeClass("black");
$(stateData[nextState][1]).addClass("black");
currState = nextState;
infinite();
}, 3000);
} else {
alert("hi");
}
}