0

我试图让它在 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");
        }

    });
});

非常感谢任何帮助,在此先感谢!

4

1 回答 1

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);
        }
    });
});
于 2013-04-18T22:03:14.457 回答