setInterval()时间是......关闭?

时间:2014-06-27 13:32:45

标签: javascript html

我对此感到困惑......但是看起来我过于复杂的setInterval()行系统已经过时了。也许时间正在努力追赶?它有点奇怪,我不知道为什么。

http://jsfiddle.net/Dr3amTw1st/HV3fA/embedded/result/

var number=0; // Do not change!
var key1=true, key2=false, key3=false, key4=false, key5=false; // Do not change!
var key6=false, key7=false, key8=false, key9=false; // Do not change!
var climbSpeed=2000; // Climbing speed in milliseconds. Don't change unless you REALLY know what you're doing.
var raised=100; // The amount raised so far. Able to be changed.
var goal=250; // The goal, obviously... :P Able to be changed.

var c1=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
var c2, c3, c4, c5, c6, c7, c8, c9, c10;

var check=setInterval(
function() {
    if (number>0 && key1==true) {
        key1=false;
        clearInterval(c1);
        climbSpeed=1500;
        c2=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key2=true;
    }
    if (number>3 && key2==true) {
        key2=false;
        clearInterval(c2);
        climbSpeed=700;
        c3=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key3=true;
    }
    if (number>9 && key3==true) {
        key3=false;
        clearInterval(c3);
        climbSpeed=200;
        c4=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key4=true;
    }
    if (number>(.15*raised) && key4==true) {
        key4=false;
        clearInterval(c4);
        climbSpeed=100;
        c5=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key5=true;
    }
    if (number>(.20*raised) && key5==true) {
        key5=false;
        clearInterval(c5);
        climbSpeed=90;
        c6=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key6=true;
    }
    if (number>(.30*raised) && key6==true) {
        key6=false;
        clearInterval(c6);
        climbSpeed=110;
        c7=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key7=true;
    }
    if (number>(raised-20) && key7==true) {
        key7=false;
        clearInterval(c7);
        climbSpeed=500;
        c8=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key8=true;
    }
    if (number>(raised-11) && key8==true) {
        key8=false;
        clearInterval(c8);
        climbSpeed=650;
        c9=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
        key9=true;
    }
    if (number>=(raised-10) && key9==true) {
        key9=false;
        clearInterval(c9);
        climbSpeed=900;
        c10=setInterval(function(){document.getElementById("amount").innerHTML="$"+number++;},climbSpeed);
    }
    if (number>raised) {
        clearInterval(c10);
        document.getElementById("amount").style.color="red";
        //document.getElementById("amount").style.fontSize="250px";
    }
},climbSpeed);

function stop() {
alert("climbSpeed: " + climbSpeed + "\n" + "number: " + number + "\n" + "raised: " + raised);
//clearInterval(c1);
//clearInterval(c2);
//clearInterval(c3);
//clearInterval(c4);
//clearInterval(c5);
//clearInterval(c6);
//clearInterval(c7);
//clearInterval(c8);
//clearInterval(c9);
//clearInterval(c10);
}

上面的jsfiddle看起来有点偏离css,但我认为这只是jsfiddle本身。当我将它全部作为谷歌浏览器中的一个文档运行时,它看起来很好(我只是让它在谷歌浏览器中运行)。这是一次在我自己的计算机上使用的应用程序,曾经作为一个筹款温度计。所以只关注数字和javascript。我只是希望数字看起来更顺畅。我考虑过使用百分比(.90 *提升),但它们具有相同的效果。我的时间和攀爬速度在这里发生了什么?此外,顶部的按钮仅用于调试目的。他们不会参加该计划的最终草案。

1 个答案:

答案 0 :(得分:0)

您多次设置setInterval,并且每2秒重复一次。在接下来的2秒之后,你会创建另一个setInterval逻辑部分,因此它正在运行双setInterval函数。如果您希望在保存间隔中更改数字,请使用单个setTimout功能。