0

我想在两者之间重置这个闪存,这样我就不必等到最后重新启动闪存,我该如何重置这个?

function flash() {
var arrayId = 0,
    splitSet = $('#text_original').html().split(" "),
    splitSetLength = splitSet.length;


function flashWord() {
    $("#flash_word").html(splitSet[arrayId]);
    arrayId += 1;
    var t = setTimeout(remove, 1000);
}

function remove() {
    if (arrayId < splitSetLength) {
        $("#flash_word").html(" ");
        flashWord();
    } //else reset_flash();
}
flashWord(); }

请参阅http://jsfiddle.net/HcDfS/

4

1 回答 1

1

将计时器变量设为全局变量并取消其超时(使用clearTimeout())。然后,隐藏#flash_word

我冒昧地重新实现了你的小提琴:

var timer, words;

function startFlashing() {
    words = $("#source").text().split(" ");
    showNextWord(0);
}

function stopFlashing() {
    clearTimeout(timer);
    $("#banner").text("");
}

function showNextWord(index) {
    $("#banner").text(words[index]);
    index++;
    if (index < words.length) timer = setTimeout(function () {
        showNextWord(index);
    }, 1000);
    else $("#banner").text("");
}

​ 使用 HTML:

<p id="source">This is the text to be flashed.</p>
<button onclick='startFlashing()'>Start Flashing!</button>
<button onclick='stopFlashing()'>Stop Flashing!</button>
<p id="banner"></p>​

它在这里:http: //jsfiddle.net/CZnVc/6/

于 2012-08-06T18:19:27.273 回答