简体   繁体   English

需要重置功能我该怎么做

[英]need to reset the function how do i do it

I want to reset this flash in between so that i need not have to wait till the end to restart the flash, how do i reset this? 我想在两次重置之间重新设置闪光灯,这样我就不必等到最后重新启动闪光灯,我该如何重置呢?

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(); }

please see the http://jsfiddle.net/HcDfS/ 请参阅http://jsfiddle.net/HcDfS/

Make the timer variable a global one and cancel the time-out on it (with clearTimeout() ). 将计时器变量设置为全局变量,然后取消超时(使用clearTimeout() )。 Then, hide #flash_word . 然后,隐藏#flash_word

I took the liberty of re-implementing your fiddle: 我选择重新实施您的小提琴:

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("");
}

​ With HTML: 使用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>​

And it's here: http://jsfiddle.net/CZnVc/6/ 它在这里: http : //jsfiddle.net/CZnVc/6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM