Javascript 如何将延迟合并到jquery的每个循环中?
考虑以下功能:Javascript 如何将延迟合并到jquery的每个循环中?,javascript,jquery,Javascript,Jquery,考虑以下功能: $("document").ready(function(){ var echoText = 'Hello world!'; echoText = echoText.split(''); $.each(echoText, function(key, value){ $("#content").append(value); }); }); $("document").ready(function(){ var data =
$("document").ready(function(){
var echoText = 'Hello world!';
echoText = echoText.split('');
$.each(echoText, function(key, value){
$("#content").append(value);
});
});
$("document").ready(function(){
var data = 'Hello world!';
var lastIndex = 0;
var fnEcho = function() {
if (lastIndex < data.length) {
$("#content").append(data[lastIndex]);
lastIndex++;
window.setTimeout(fnEcho, 1000);
}
}
fnEcho();
});
这只是回显输入文本。不过,我想做的是在每个回显字符之间添加一个延迟,这样看起来就像是一个缓慢的人工输入。有什么办法吗?我尝试过谷歌搜索,但到目前为止没有任何帮助
$("document").ready(function(){
var echoText = 'Hello world!';
var currentChar = 0;
var intval = setInterval(function(){
var value = echoText.charAt(currentChar);
$("#content").append(value);
currentChar++;
if(currentChar >= echoText.length){
clearInterval(intval);
}
}, 10);
});
我还没有测试过,所以可能有问题,但这是解决问题的方法。您可以增加值10以减慢文本速度。打字机 看 使用ike
$("#my-container").typewriter();
你可以从
考虑使用
setTimeout()
函数:
$("document").ready(function(){
var echoText = 'Hello world!';
echoText = echoText.split('');
$.each(echoText, function(key, value){
$("#content").append(value);
});
});
$("document").ready(function(){
var data = 'Hello world!';
var lastIndex = 0;
var fnEcho = function() {
if (lastIndex < data.length) {
$("#content").append(data[lastIndex]);
lastIndex++;
window.setTimeout(fnEcho, 1000);
}
}
fnEcho();
});
$(“文档”).ready(函数(){
var data='Hello world!';
var lastIndex=0;
var fnEcho=函数(){
if(lastIndex
试试这个:
var printNextChar = function(theStr, pos){
pos = (typeof(pos) == 'undefined') ? 0 : pos;
$('#content').append(theStr.substr(pos,1));
++pos;
if(pos <= theStr.length){
setTimeout(function(){printNextChar(theStr,(pos));},300);
}
}
$("document").ready(function(){
var echoText = 'Hello world!';
printNextChar(echoText);
});
var printNextChar=函数(theStr,pos){
pos=(typeof(pos)=‘未定义’)?0:pos;
$(“#content”).append(theStr.substr(pos,1));
++pos;
if(pos这里有一个使用$的解决方案。动画和步骤
function SimulateTyping(containerID, data) {
var dv = $('#' + containerID), len = data.length;
dv.text("");
$({count: 0}).animate({count: len}, {
duration: len * 15,
step: function() {
dv.text(data.substring(0, Math.round(this.count)));
}
});
}
就像
SimulateTyping("content", someText);
演示:@Demian-别忘了“重塑车轮”每次都会让车轮更圆一点!@roXon我不同意!我见过太多形状怪异的重塑车轮,不同意……@Ivar,好吧,比使用你喜欢的车轮更合适(有很多……在某些情况下,你的意见可能是主观的。)但是要小心选择合适的轮胎…轮辋…压力…或者只是重新设计车轮。它可能最适合你的汽车;D