清除间隔如果(条件)
clearInterval if (condition)
我会如果(NArray === 2){clearInterval()},但我不知道该怎么做,因为如果我把它放在setInterval中,它显然不起作用,如果我把它放在外面也是一样。谢谢大家
$(function Intervallo(){
var NArray = -1;
var ringraziamenti = [
"",
"Thanks",
"You saved my Website",
"Now you can use it, and try to find all the hidden tresure that there are",
"Good Luck",
];
$("#Save").click(function() {
if (NArray === -1) {
clearInterval();
}
setTimeout(function() {
$("head").append('<link href="https://fonts.googleapis.com/css?family=Italianno" rel="stylesheet" type="text/css"/><style>body {font-family:"Italianno", cursive; font-weight:800; font-size:36px;}</style>');
}, 3000);
setInterval(function () {
NArray = NArray + 1;
$("#magia").remove();
$("#BGimage").css("background-image", "url(../../Desktop/Senza%20titolo-1.png)");
$("#ringraziamenti").html(ringraziamenti[NArray]);
}, 3000);
});
});
一个可能的解决方案是:
$(function Intervallo() {
// declare variable to save the return value of setInterval
var interval = null;
var NArray = -1;
var ringraziamenti = [
"",
"Thanks",
"You saved my Website",
"Now you can use it, and try to find all the hidden tresure that there are",
"Good Luck",
];
$("#Save").click(function () {
if (NArray === -1) {
if (interval != null) {
clearInterval(interval);
interval = null;
}
}
setTimeout(function () {
$("head").append('<link href="https://fonts.googleapis.com/css?family=Italianno" rel="stylesheet" type="text/css"/><style>body {font-family:"Italianno", cursive; font-weight:800; font-size:36px;}</style>');
}, 3000);
// save the return value of setInterval
interval = setInterval(function () {
NArray = NArray + 1;
if (NArray == 2) { // when NArray is 2 stop setInterval
NArray = -1;
clearInterval(interval);
interval = null;
}
$("#magia").remove();
$("#BGimage").css("background-image", "url(../../Desktop/Senza%20titolo-1.png)");
$("#ringraziamenti").html(ringraziamenti[NArray]);
}, 3000);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="Save">Save</button>
相关文章:
- Javascript,如果条件在没有&&逻辑运算符当&&它不起作用
- 如果条件匹配,则停止执行脚本
- 如果满足某些条件,我如何才能创建一个只有值的对象
- 如果条件为循环-性能差异
- 如果满足某些条件,如何在不调用模态的情况下首先调用类
- 根据条件将函数放入数组中(如果选中复选框)
- 如果($_SERVER[“REQUEST_METHOD”]=“POST”)条件在ajax调用不同的php文件进行验证和提
- 如果函数PHP中的条件为true,则禁用链接
- 如果条件将长 JavaScript 拆分为多行是否安全
- 如果条件在代码点火器的模型中不起作用
- 如果条件推送包含/匹配字符串的值
- Ember.js:如果控制器中满足某些条件,则在路由器中重定向
- Javascript 'while' 循环,如果条件为 false,如何停止迭代
- JavaScript / jquery:如果选择了单选按钮并且数据属性符合条件,则添加类
- 如果条件,则在其他操作之后调用 Redux 操作
- 如果只有一次有条件,如何执行
- Jquery - 如果条件为真,则遍历选择器并执行某些操作
- JavaScript条件(如果并行)
- 更好的性能?200+ 个单独的结果页面或 200+ 个条件(如果还有其他梯形图或开关大小写?)在 Javascript/
- js条件:如果不是false、null或undefined,那么