是否可以在javascript中将for循环与setTimeOut组合在一起
Is it possible to combine a for loop with setTimeOut in javascript?
我很清楚在这里做错了什么。所有字母一次打印出来。我想要 HTML 画布中的打字机效果。(最终,我希望字母遵循画布内描摹的线条,但这是一个更大的难题。我认为问题出在 for 循环中,但我不确定如何解决它......任何帮助表示赞赏!
<html>
<head>
<style>
canvas {
border: 2px solid #f00;
}
</style>
</head>
<label>Your message here:</label>
<br>
<input class="textBox" style="width: 50em; height: 2em;" id="userInput" />
<button id="sub_btn">Submit</button>
<body>
<canvas width="360" height="400" id="canvas"></canvas>
<script type="text/javascript">
var canvas, ctx;
function typeOut(str, startX, startY, lineHeight, padding) {
var random = Math.floor((Math.random()*100)+40);
var cursorX = startX || 0;
var cursorY = startY || 0;
var lineHeight = lineHeight || 32;
padding = padding || 10;
var i = 0;
for (i = 0; i <= str.length; i++){
var w = ctx.measureText(str.charAt(i)).width;
if(cursorX + w >= canvas.width - padding) {
cursorX = startX;
cursorY += lineHeight;
}
if (str.charAt(i) === ''n' || str.charAt(i) === ''r'){
ctx.fillText(str.charAt(i), cursorX, cursorY);
setTimeout(typeOut, 20);
cursorX = startX;
} else if (str.charAt(i) === ' '){
ctx.fillText(str.charAt(i), cursorX, cursorY);
setTimeout(typeOut, 20);
cursorX += w;
} else if (str.charAt(i) === ',' || str.charAt(i) === '.' || str.charAt(i) === '?'){
ctx.fillText(str.charAt(i), cursorX, cursorY);
setTimeout(typeOut, 220);
cursorX += w;
} else if ( str.charAt(i) === '@') {
;
setTimeout(typeOut, 300);
} else {
ctx.fillText(str.charAt(i), cursorX, cursorY);
setTimeout(typeOut, random);
cursorX += w;
}
}
}
(document.getElementById('sub_btn').onclick = function() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
ctx.fillStyle = '#000000';
ctx.font = '24px sans-serif';
var str = document.getElementById('userInput').value;
typeOut(str, 25, 25, 32, 10 );
})();
</script>
</body>
</html>
setTimeout()
不像"睡眠"函数。它不会暂停脚本。当你说:
setTimeout(typeOut, 220);
你说的是"在 220 毫秒内安排另一个呼叫typeOut()
,同时像以前一样继续for
循环。
你想要处理一个字符,然后调用一个延续函数(以正确的间隔)来处理下一个字符。 像这样:
function typeOut(str, startX, startY, lineHeight, padding) {
var random = Math.floor((Math.random()*100)+40);
var cursorX = startX || 0;
var cursorY = startY || 0;
var lineHeight = lineHeight || 32;
padding = padding || 10;
var i = 0;
// this will be called once per character
var processNext = function() {
if (i >= str.length)
return;
var timeout = random;
var w = ctx.measureText(str.charAt(i)).width;
if(cursorX + w >= canvas.width - padding) {
cursorX = startX;
cursorY += lineHeight;
}
if (str.charAt(i) === ''n' || str.charAt(i) === ''r'){
ctx.fillText(str.charAt(i), cursorX, cursorY);
timeout = 20;
cursorX = startX;
} else if (str.charAt(i) === ' '){
ctx.fillText(str.charAt(i), cursorX, cursorY);
timeout = 20;
cursorX += w;
} else if (str.charAt(i) === ',' || str.charAt(i) === '.' || str.charAt(i) === '?'){
ctx.fillText(str.charAt(i), cursorX, cursorY);
timeout = 220;
cursorX += w;
} else if ( str.charAt(i) === '@') {
timeout = 300;
} else {
ctx.fillText(str.charAt(i), cursorX, cursorY);
cursorX += w;
}
++i;
setTimeout(processNext, timeout);
};
// now start typing
processNext();
}
示例代码笔:http://codepen.io/paulroub/pen/EDqok
相关文章:
- JS-我的循环使用setTimeout停止循环
- javascript:在for循环中使用settimeout来定期显示文本
- 如何让 setTimeout 函数运行,然后停止使用循环
- nodejs-settimeout是否在不同的循环中共享变量
- 在for循环中使用setTimeout失败
- 为什么javascript setTimeout()不能在循环中工作
- 如何将setTimeout与“;当“;循环
- 而循环调用setTimeout不起作用
- for循环中嵌套if条件的setTimeout
- Javascript:setTimeout,用于循环和回调函数
- 在循环中调用 setTimeout 未按预期工作
- Jquery 每个循环都无法提取值,除非使用 setTimeout
- 使用 setTimeout() 循环,使矩形变大,直到它到达画布边缘,然后重置
- 我可以在 for 循环中设置多个 window.setTimeout 事件吗?
- Firefox setTimeout + jQuery 淡入淡出循环不一致,提前停止,不会循环
- 为什么我的循环在迭代之间没有等待?setTimeout()
- setTimeout 会创建一个无限循环
- setTimeout 在循环中,它的行为与预期不同
- 循环中 setTimeout 的 Jquery 用法
- 我想循环setTimeout()函数后,每22秒