我从值 i=0 开始,最大值为 4,当脚本检测到值与 4 相同时,必须重新启动或将“i”的值重置为 0 作为默认值
i = 0;
function loading() {
if (i == 4) {
i = 0;
}
jQuery("#text").show(1000).hide(1000, function() {
reloading();
});
}
function reloading() {
setInterval("loading()", 2000);
alert("ok" + i);
i++;
}
loading();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text" style="position:relative;width:150px;height:150px;background-color:red;display:none;"></div>
我尝试将 div 显示 4 次并重置为 0 并在其他时间 0、1、2、3 开始,然后重置 0 并在其他时间开始,但不起作用,我不知道为什么会这样,我认为代码还可以,但肯定有问题
感谢高级的帮助
最佳答案
问题是当您调用函数 loading()
来启动动画变量时 i
值从 1 开始,而不是 0,因此它只进行了 3 次并且然后重置为 0。(因为经过 3 次迭代 i
变为 4)
所以在 if(i==4){
中将 4 增加到 5,你就可以开始了
工作片段:-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="text" style="position:relative;width:150px;height:150px;background-color:red;display:none;"></div>
<script>
i=0;
$(document).ready(function(){
loading();
});
function loading(){
if(i==5){
i=0;
}
jQuery("#text").show(1000).hide(1000,function(){reloading();});
}
function reloading(){
setInterval("loading()",2000);
alert("ok"+i);
i++;
}
</script>
关于javascript - Jquery自动增量值和检测重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50362412/