箭头功能不是一个功能

时间:2016-09-21 08:56:00

标签: javascript

我收到以下错误TypeError: f is not a function:我做错了什么?

var expressws = require("express");
var appws = expressws();
var expressWs = require('express-ws')(appws);

function x(ws)
{
    console.log("send");
}

function loop(f,t)
{
    setTimeout(() => loop(), t);
    f();
}

appws.ws('/', function(ws, req) {
    ws.on('message', function(msg){
        var data = JSON.parse(msg);
        if(data.action == "Logged in"){
            loop(() => x(ws),1000);
        }
    });

});

2 个答案:

答案 0 :(得分:2)

以下间接递归调用不会将任何内容作为参数传递给loop,因此会出错。

setTimeout(() => loop(/* should pass two arguments here */), t);

答案 1 :(得分:0)

您需要将参数传递给循环函数

setTimeout(() => loop(f, t), t);

另一种方法是

setTimeout(loop.bind(null, f, t), t);

或lodash版

_.delay(loop, t, f, t);