Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 理解Js函数中的回调_Javascript - Fatal编程技术网

Javascript 理解Js函数中的回调

Javascript 理解Js函数中的回调,javascript,Javascript,好的,这是一个基本的问题,但作为一个从未使用过回调函数的人,在经历了多次失败后,我想也许你们可以帮助我 我们考虑下面的例子: 加载html时,函数showtext会显示作为参数传递的文本(只是一个小而漂亮的动画)。当这个函数完成时,当所有的句子都显示出来时,我想调用另一个函数showAuthor,它显示了上述文章的作者 function showText(target, message, index, interval, callback) { if (index < messag

好的,这是一个基本的问题,但作为一个从未使用过回调函数的人,在经历了多次失败后,我想也许你们可以帮助我

我们考虑下面的例子:

加载html时,函数
showtext
会显示作为参数传递的文本(只是一个小而漂亮的动画)。当这个函数完成时,当所有的句子都显示出来时,我想调用另一个函数
showAuthor
,它显示了上述文章的作者

function showText(target, message, index, interval, callback) {
    if (index < message.length) {
        $(target).append(message[index++]);
        setTimeout(function() { showText(target, message, index, interval); }, interval);
    }
    if (callback && typeof(callback) === "function") {
        callback();
    }

}

function showAuthor() {
    var name = '<span>as posted by someone</span>';
    $(name).hide().appendTo(".author").fadeIn(300);
}

$(function() {
    showText("#aboutMeMsg", "Some VERY cool text here", 0, 100, showAuthor());
});
函数showText(目标、消息、索引、间隔、回调){
if(索引<消息长度){
$(目标).append(消息[index++]);
setTimeout(函数(){showText(目标、消息、索引、间隔);},间隔);
}
if(回调和类型化(回调)=“函数”){
回调();
}
}
函数showAuthor(){
变量名称='由某人发布';
$(name).hide().appendTo(“.author”).fadeIn(300);
}
$(函数(){
showText(“#aboutMeMsg”,“这里有一些非常酷的文本”,0100,showthor());
});
问题 上述代码的问题在于,函数
showAuthor
在函数
showtext
启动时同时执行。这意味着
showAuthor
能够在
showText
完成之前完成

我认为问题在于我使用的函数
showText
是递归的,但我无法解决这个问题


现在,为什么会发生这种情况,我无法理解……

您需要在没有括号的情况下通过showAuthor。因为使用括号意味着您正在调用该函数。如下所示:

$(function() {
    showText("#aboutMeMsg", "Some VERY cool text here", 0, 100, showAuthor);
});
然后,在递归调用
showText
函数时,需要传递相同的
回调

编辑:

希望这会有所帮助

    function showText(target, message, index, interval, callback) {
      if (index < message.length) {
        $(target).append(message[index++]);
        setTimeout(function() { showText(target, message, index, interval, callback); }, interval);
      } else if (callback && typeof(callback) === "function") {
        callback();
      }
    }

    function showAuthor() {
      var name = '<span>as posted by someone</span>';
      $(name).hide().appendTo(".author").fadeIn(300);
    }

    $(function() {
      showText("#aboutMeMsg", "Some VERY cool text here", 0, 100, showAuthor);
    });
函数showText(目标、消息、索引、间隔、回调){
if(索引<消息长度){
$(目标).append(消息[index++]);
setTimeout(函数(){showText(目标、消息、索引、间隔、回调);},间隔);
}else if(回调和类型化(回调)=“函数”){
回调();
}
}
函数showAuthor(){
变量名称='由某人发布';
$(name).hide().appendTo(“.author”).fadeIn(300);
}
$(函数(){
showText(#aboutMeMsg”,“这里有一些非常酷的文本”,0,100,showthor);
});

实际上,您需要在
showText
函数中传递回调函数引用
showAuthor
,而不是调用它
showAuthor()

,首先您应该按照前面的回答更正回调传递。所以应该是这样,

$(function() {
    showText("#aboutMeMsg", "Some VERY cool text here", 0, 100, showAuthor);
});
然而,在
showText
函数中,当您调用回调时,它会同步执行,这就是问题所在。您应该将回调发送到事件队列的末尾,以便它在
showText
函数之后运行。您可以尝试更改以下行

if (callback && typeof(callback) === "function") {
  callback();
}


您试图传递调用的回调,因此实际上您并没有将
showAuthor
函数作为回调传递,而是将其返回值作为参数传递给
showText
函数。(可能为“未定义”)@Redu是的,但我在firebug控制台上没有收到任何错误。您没有收到任何错误,因为根据vm的情况,没有任何错误。这是关于语义的。您正在将
showAuthor
函数的返回值传递给
showText
函数,该函数可能是
未定义的
showText
函数,并且在
showText
函数中,使用
if(回调和类型(回调)==“函数”){callback callback();}检查
callback
它的计算结果为false。您应该传递给定答案中所示的
showAuthor
函数定义。我的意思是没有调用参数。请注意,即使回调正常工作,它也不会产生您描述的效果,因为它将在第一次调用showText()后被调用。您能用我的示例解释调用该函数的
的含义吗?它没有起作用expected@GeorgeGkas
setTimeout(函数(){showText(目标、消息、索引、间隔、回调);},间隔)是,您对
else
语句的看法是正确的。我讨厌逻辑错误。您只需在递归调用的
showText
函数中添加回调参数。编辑它,我会接受你。
if (callback && typeof(callback) === "function") {
  callback();
}
if (callback && typeof(callback) === "function") {
  setTimeout(callback,0);
}