-1
該代碼旨在以打字方式動畫某些文本。我希望它只運行一次,但它始終在句子區域循環。我將如何去阻止它循環。代碼的頂部收集輸入的值並放入數組,這一切都很好。這只是循環我有問題。這個Javascript函數保持循環,我怎樣才能讓它運行一次?
var yourWord = document.getElementById("myText").value;
var yourtext = "I took the word " + yourWord + "...";
var infomation = [yourtext,
'I looked at the most relevant news article relating to it...',
'And created this piece of art from the words in the article! '
],
part,
i = 0,
offset = 0,
pollyLen = Polly.length,
forwards = true,
skip_count = 0,
skip_delay = 5,
speed = 100;
var wordflick = function() {
setInterval(function() {
if (forwards) {
if (offset >= infomation[i].length) {
++skip_count;
if (skip_count == skip_delay) {
forwards = false;
skip_count = 0;
}
}
} else {
if (offset == 0) {
forwards = true;
i++;
offset = 0;
if (i >= pollyLen) {
i = 0;
}
}
}
part = infomation[i].substr(0, offset);
if (skip_count == 0) {
if (forwards) {
offset++;
} else {
offset--;
}
}
$('#pollyInfo').text(part);
}, speed);
};
$(document).ready(function() {
wordflick();
});
具體循環是什麼?我的意思是,有setInterval,它重複,但這是它的重點。我沒有看到任何其他循環。 –
如果你不想讓它循環,不要使用'setInterval()'。只需執行一次代碼,然後在您想要再次執行時調用該函數。 – Barmar
句子的數組保持循環。我想讓它循環一遍,然後停下來。 –