简体   繁体   English

Meteor.setTimeout函数不起作用

[英]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. 我只是想将GoogleMap函数的触发时间设置为2秒,但它不起作用。 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. 我已经尝试了很多事情,例如为我的函数声明一个var,然后匿名触发setTimeout函数,等等。。。但是没有运气。我没有从控制台收到错误,所以我觉得我的代码写得很好Meteor文档并没有提供太多有关setTimeout函数的信息。

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
      });
    });
  });
});

Take your code inside setTimeout to a external function like this: 将您的代码在setTimeout中移到一个外部函数,如下所示:

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: 然后调用setTimeout内部的函数, 不带括号 ,如下所示:

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. 如果使用括号调用该函数,则该函数将立即执行,而不会在timeOut中指定延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM