I'm getting the error TypeError: f is not a function for the following: What am I doing wrong?
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);
}
});
});
asked Sep 21, 2016 at 8:55
Baz
13.3k40 gold badges153 silver badges279 bronze badges
2 Answers 2
The following indirect recursive call passes nothing as an argument to loop, hence the error.
setTimeout(() => loop(/* should pass two arguments here */), t);
answered Sep 21, 2016 at 8:58
Ben Aston
56.1k69 gold badges221 silver badges352 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You need to pass arguments into loop function
setTimeout(() => loop(f, t), t);
another approach is
setTimeout(loop.bind(null, f, t), t);
or lodash version
_.delay(loop, t, f, t);
answered Sep 21, 2016 at 9:08
hawk
5,4082 gold badges25 silver badges29 bronze badges
Comments
lang-js
function loop(f,t){ setTimeout(() => loop(), t);f();}.setTimeout(() => { loop(f,t); }, t);