Boogieprod
Boogieprod

Reputation: 11

Meteor.setTimeout function doesn't work

I've looked around the internet for quite a while trying to figuring out what's wrong with my code and couldn't find a working answer.

Template.map.onCreated(function() {
    Meteor.setTimeout(function() {
      GoogleMaps.ready('exampleMap', function(map) {
        var marker = new google.maps.Marker({
          position: map.options.center,
          map: map.instance
        });
      });
    }, 2000);
  });

I'm simply trying to set a 2 seconds delay for the GoogleMap function to trigger but it doesn't work. I've tried a lot of various things like declaring a var to my function and then trigger the setTimeout function anonymously, etc... But no luck... I don't get errors from console so I feel my code is well written and Meteor docs doesn't provide much information on the setTimeout function.

This doesn't work as well:

Template.map.onRendered(function() {
  Tracker.afterFlush(function(){
    GoogleMaps.ready('exampleMap', function(map) {
      var marker = new google.maps.Marker({
        position: map.options.center,
        map: map.instance
      });
    });
  });
});

Upvotes: 1

Views: 998

Answers (1)

Ruben
Ruben

Reputation: 836

Take your code inside setTimeout to a external function like this:

function prepareMap() {
      GoogleMaps.ready('exampleMap', function(map) {
        var marker = new google.maps.Marker({
          position: map.options.center,
          map: map.instance
        });
      });
    }
 }

And call the function inside the setTimeout without parenthesis, like this:

Template.map.onCreated(function() {
   setTimeout(prepareMap, 2000);
});

If you call the function using parenthesis the function will be executed immediately without the delay specified in the timeOut.

Upvotes: -1

Related Questions