我有以下功能登录通过在某一给定时间的函数是句子中的人物,看看下面的功能:For循环不允许的setTimeout执行
function print_string(param , timer) {
var strt_point = 0,
str_len = param.length,
self = this;
//typewritter();
typeit();
function typeit() {
setTimeout(function(){
console.log(param.substring(strt_point).charAt(0));
if(strt_point < str_len) {
strt_point++;
typeit()
}
} , timer);
}
}
var str = print_string("hey there , whats up , hows the weather in Manhatten" , 50);
console.log(str);
以上程序员工作正常,现在如果我添加一个for循环到上面的程序IE 包裹的setTimeout在for循环中,
function print_string(param , timer) {
var strt_point = 0,
str_len = param.length,
self = this;
for(var i = 0 ; i < str_len ; i++) {
//typewritter();
typeit();
function typeit() {
setTimeout(function(){
console.log(param.substring(strt_point).charAt(0));
if(strt_point < str_len) {
strt_point++;
typeit()
}
} , timer);
}
}
var str = print_string("hey there , whats up , hows the weather in Manhatten" , 50);
console.log(str);
所有的人物在一次打印,为什么呢?
为什么for循环不符合setTimeout
间隔?有人可以解释吗?
你有一个函数,这是不允许的内部函数,函数typeit是内部功能print_string –
岂不等于说他们调用关闭?任何文件来证明你的话是什么?我是一个JS新手,所以不是专家本人! –
这真是毫无根据的@BhawinParkeria – charlietfl