15

In Meteor, why would one use Meteor.setTimeout() over just normal setTimeout()?

What is the value of using Meteor.setTimeout() instead of just the vanilla setTimeout or setInterval?

Mcope
  • 781
  • 8
  • 22

2 Answers2

20

On the client, there's no difference between them.

On the server, when code is running for a specific user (for example in method calls), you need to use Meteor.setTimeout instead of window.setTimeout to make Meteor remember for which user the function should be called. In the time between the function passed to Meteor.setTimeout is called and when it's called, other users may have called methods on the server, chancing Meteor.userId to return their user id instead. Meteor.setTimeout will change back so Meteor.userId return the user id for the user the call to Meteor.setTimeout was made before calling the function passed to it.

It is a design decision.

Peppe L-G
  • 7,351
  • 2
  • 25
  • 50
9

Using Meteor.setTimeout() ensures that this code is Fibers aware. Read more about Fibers: https://github.com/laverdet/node-fibers

Mário
  • 1,603
  • 14
  • 24
  • Thank you, would you be so kind to provide an example on what kind of new things Meteor.setTimeout() can do with fiber as opposed to without? – Mcope Jan 09 '15 at 01:34
  • You have to understand how node works and what fibers allow. Check this answer by Slava (A Meteor core developer): https://groups.google.com/d/msg/meteor-talk/uipZ9I_37AU/SimAAlYnY6sJ – Mário Jan 09 '15 at 01:48