javascript - 我需要整个类型的文字在第二个循环消失之前在页面上停留一段时间
问题描述
1 . 一旦在第二个循环之前完全写入,我需要整个类型的书写文本留在显示屏上。请在我的代码上应用解决方案。
2 . 其次,在我的文本字符串中,当我使用“b”或“strong”标签将特定文本变为粗体时,在键入过程中会显示“<”符号几毫秒,所有其他标签都会发生同样的事情。我不知道我的代码有什么问题。
下面是我的代码。
for (let i = 0; i < 10; i++) {
task(i);
}
function task(i) {
setTimeout(function() {
// Add tasks to do
var typeString = ['• I m Mr.Frits.\n• and I <b>love </b> Pakistan...:)'];
var i = 0;
var count = 0
var selectedText = '';
var text = '';
(function type() {
if (count == typeString.length) {
count = 0;
}
selectedText = typeString[count];
text = selectedText.slice(0, ++i);
document.getElementById('typing').innerHTML = text.fontsize(6);
document.getElementById('typing').style.fontFamily = "monospace";
document.getElementById("typing").style.color = "black";
document.getElementById("typing").style.fontWeight = "normal";
if (text.length === selectedText.length) {
count++;
i = 0;
}
/* SOLUTION : wait two seconds when new line */
if (typeString[0][i - 1] == '\n') {
setTimeout(type, 1000);
} else {
setTimeout(type, 100);
}
}());
}, 1000);
}
<pre id="typing"></pre>
解决方案
由于 count 设置为1
一旦达到字符串末尾的长度,您可以添加条件并在满足时增加超时:
/* SOLUTION : wait two seconds when new line */
if (typeString[0][i - 1] == '\n') {
setTimeout(type, 1000);
} else if (count === 1) {
setTimeout(type, 3000);
} else {
setTimeout(type, 100);
}
使用<br />
's,浏览器在标记完成之前不会将其注册为有效的 HTML。因此,有那么一秒钟,所有呈现的内容都<
在标记的其余部分完成之前,并且它了解该标记是什么。
for (let i = 0; i < 10; i++) {
task(i);
}
function task(i) {
setTimeout(function() {
// Add tasks to do
var typeString = ['• I m Mr.Frits.\n• and I love Pakistan...:)'];
var i = 0;
var count = 0;
var selectedText = '';
var text = '';
var typing = document.getElementById('typing');
(function type() {
if (count == typeString.length) {
count = 0;
};
selectedText = typeString[count];
text = selectedText.slice(0, ++i);
typing.innerHTML = text.fontsize(6);
typing.style.fontFamily = "monospace";
typing.style.color = "black";
typing.style.fontWeight = "normal";
if (text.length === selectedText.length) {
count++;
i = 0;
}
/* SOLUTION : wait two seconds when new line */
if (typeString[0][i - 1] == '\n') {
setTimeout(type, 1000);
} else if (count === 1) {
setTimeout(type, 3000);
} else {
setTimeout(type, 100);
}
}());
}, 1000);
}
<pre id="typing"></pre>
推荐阅读
- c# - 数据可视化图表轴动态位置
- python - 将一组标头添加到现有数据帧
- c# - DataGrgid DataRowTemplate 中的当前上下文中不存在 TextBox
- php - 如何对mysql数据进行排序并找到位置?
- crystal-reports - Crystal Report如何删除带有“字符串”的行
- apache - Apache 2 绑定端口失败,端口已打开
- javascript - JS for -loop 总是使用最后一个元素 - 范围问题
- css - 静态引导导航栏在较小的屏幕上不会达到 100% 的宽度
- docker-compose - docker-compose syslog 驱动程序无法正常工作
- gradle - gradle 一直崩溃,已经尝试删除 gradle 文件夹,甚至尝试重新安装我所有的 IDE,包括 java、flutter 等