如何使用settimeout依次实现两个打字机效果,从而通过延迟字符和延迟行通过延迟字符打印行字符?
我正在尝试建立一种方法,以使网站上的多条字符串“键入”。我一直在为每一行编写一个新功能,但是现在我试图设置一个函数以循环循环通过一系列字符串,然后在新行中键入每个字符串。但是,我无法正确暂停,只能获得第一个字符串,而第三字符串的一部分则可以实际输入。有没有更好的方法来计算超时,或者有一种更好的方法可以使功能连续运行而不是同时运行?我正在尝试坚持使用香草JavaScript,所以请不要jQuery。
var typeSpeed = 75;
var newSpan = '<span></span><br>';
function newTextSegment() {
i = 0;
j = 0;
stringsArray = ['Type Line 1.', 'Type Line 2.', 'Type Line 3.'];
arrayLength = stringsArray.length;
typeLinesConsecutively();
}
function typeLinesConsecutively() {
if (j < arrayLength) {
segmentString = stringsArray[j];
delay = segmentString.length * typeSpeed;
document.querySelector('body').insertAdjacentHTML('beforeend', newSpan);
typewriter();
j++;
setTimeout(typeLinesConsecutively, delay);
}
}
//This is the function that will type out letter by letter
function typewriter() {
var allSpan = document.querySelectorAll('span');
if (i < segmentString.length) {
allSpan[allSpan.length - 1].innerHTML += segmentString.charAt(i);
i++;
setTimeout(typewriter, typeSpeed);
}
}
window.onload = (event) => {
console.log('page loaded');
newTextSegment();
};
I'm trying to build out a way to have multiple lines of strings "typed" out on a website. I've been writing a new function for every line, but now I'm trying to setup one function to loop through an array of strings, and type each string out on a new line. However, I can't get the timeouts right and only get the first string, and part of the 3rd string to actually type out. Is there a better way to calculate the timeouts, or a better way to get the functions to run consecutively rather than at the same time? I'm trying to stick with vanilla javascript, so no jquery please.
var typeSpeed = 75;
var newSpan = '<span></span><br>';
function newTextSegment() {
i = 0;
j = 0;
stringsArray = ['Type Line 1.', 'Type Line 2.', 'Type Line 3.'];
arrayLength = stringsArray.length;
typeLinesConsecutively();
}
function typeLinesConsecutively() {
if (j < arrayLength) {
segmentString = stringsArray[j];
delay = segmentString.length * typeSpeed;
document.querySelector('body').insertAdjacentHTML('beforeend', newSpan);
typewriter();
j++;
setTimeout(typeLinesConsecutively, delay);
}
}
//This is the function that will type out letter by letter
function typewriter() {
var allSpan = document.querySelectorAll('span');
if (i < segmentString.length) {
allSpan[allSpan.length - 1].innerHTML += segmentString.charAt(i);
i++;
setTimeout(typewriter, typeSpeed);
}
}
window.onload = (event) => {
console.log('page loaded');
newTextSegment();
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OP的递归延迟功能的方法可以完全坚持一个任务,然后需要以所有逻辑(是否呈现新线路和/或键入新字符)的方式实现,该任务必须由它。
为了提供解决方案和特定特定任务的解决方案,必须选择一种较高抽象的方法。
下一个提供的一个基于两个一般实现的函数...
promise
,重复使用这两个函数两次启用一个解决方案,该解决方案迭代延迟
line
值(具有新的行延迟),每行迭代迭代延迟char> char> char
values(带有特定于字符的特定于字符) (键入)延迟)两次通过for-wait ... 。
来调用这两个功能
The OP's approach of a recursively delayed function forces one to stick to exactly a single task which then needs to be implemented in a way that all logic (whether to render a new line and/or to type a new character) has to be carried by it.
In order to provide a solution with line and character specific tasks one has to choose an approach of higher abstraction.
The next provided one is based on two generically implemented functions ...
Promise
,Re-using this two functions twice enables a solution which iterates deferred
line
values (with a new line specific delay) and for each line iterates deferredchar
values (with a character specific (typing) delay) both times byfor-await...of
.Both functions can be invoked with optional settings for e.g.