JQuery如果命令不起作用

时间:2013-04-18 21:49:15

标签: jquery if-statement click var

我正在努力使它在currentLetter变量达到34时颜色发生变化但是由于某种原因它只是在我点击后立即改变颜色。

var isStart = false;
var letterString = "D0E916C0A8CED059359C02DARREN KORB - PALE WATCHERS.mp3E415AAB0014400E104B40DE96A0";
var letters = letterString.split('');
var currentLetter = 0;


function changeLetter() {
    $("#t-" + currentLetter).text(letters[currentLetter - 1]);
    currentLetter += 1;

}

$(document).ready(function () {
    var image = $('#content').click(function () {
        if (!isStart) {
            isStart = true;
            setInterval(changeLetter, 100);
        }


        if (currentLetter == 34) {
            $("#t-1").css("color", "red");
        }

    });
});

非常感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:1)

将if移动到changeLetter函数中:

function changeLetter() {
    $("#t-"+currentLetter).text(letters[currentLetter-1]);
    currentLetter +=1 ;
    if (currentLetter == 34) {
       $("#t-1").css("color", "red");          
    }

}

请注意,您应该保留间隔ID,否则您将无法阻止它。

var isStart = false;
var letterString = "D0E916C0A8CED059359C02DARREN KORB - PALE WATCHERS.mp3E415AAB0014400E104B40DE96A0";
var letters = letterString.split('');
var currentLetter = 0;
 var intervalID;

function changeLetter() {
    $("#t-"+currentLetter).text(letters[currentLetter-1]);
    currentLetter +=1 ;
    if (currentLetter == 34) {
           $("#t-1").css("color", "red");    
           clearInterval(intervalID);       
        }
}

$(document).ready(function() {
    var image = $('#content').click(function() {
        if  (!isStart) {
            isStart = true;
           intervalID =  setInterval(changeLetter, 100);
        }
    });
});