0

I just noticed a strange behavior while using setInterval. I'm using setInterval to request the server to send data every 2 seconds, and this is what I quite naturally proceeded to do:

this.socketInterval = setInterval(this.socketio.emit, 2000, 'persist'); // didn't work

I thought it would do the trick as,

this.socketio.emit('persist');

had worked. Instead it threw an error

Uncaught TypeError: Cannot read property 'push' of undefined

I got it to work by encapsulating it within a function, but I am still curious about why simply passing this.socketio.emit as first argument didn't work.

this.socketInterval = setInterval(p => this.socketio.emit(p), 2000, 'persist'); // works!
いちにち
  • 417
  • 6
  • 16

0 Answers0