需要重置功能我该怎么做

时间:2012-08-06 18:13:31

标签: javascript jquery

我想在两者之间重置此闪光灯,这样我就不必等到最后重新启动闪光灯,我该怎么重置它?

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/

1 个答案:

答案 0 :(得分: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/

相关问题