未定义此回调函数 [英] Getting undefined for this callback function

查看:475
本文介绍了未定义此回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

prepOutput()是一个简单的函数,它接受一个平均值并返回回调输出(),但我在return callback(out)得到一个未定义的错误。为什么回调不工作?

prepOutput() is a simple function that takes an average and returns the callback output(), but I am getting an undefined error at "return callback(out)". Why is the callback not working?

    function finish() {
        isRunning = false;
        prepOutput(avgSpeed(),output());

    }

    function avgSpeed() {
        var avg = (totReactt/numClick);
        return avg.toFixed(2);
    }

    function prepOutput(avgS, callback){
        var out = "Your averege speed is " + avgS;

        return callback(out);

    }

    function output(x) {
        alert("Thank you for playing! " + x);
    }


推荐答案

引用作为回调的值,而是调用输出,并传递从它返回的值( undefined 没有返回值)作为回调的值参数 prepOutput

You need to pass a function reference as the value for callback, instead you are invoking output and is passing the value returned from it(undefined as there is no value returned) as the value for callback argument to prepOutput

prepOutput(avgSpeed(), output);

问题:演示 - 输出前调用prepOutput

演示:小提琴

Problem: Demo - output is called before prepOutput
Demo: Fiddle

这篇关于未定义此回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆