0

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
2
  • 1
    You are calling a function which is not present in the code . In this line. function loop(f,t){ setTimeout(() => loop(), t);f();}. Commented Sep 21, 2016 at 8:58
  • 1
    setTimeout(() => { loop(f,t); }, t); Commented Sep 21, 2016 at 8:59

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.