Meteor.setTimeout 函数不起作用 [英] Meteor.setTimeout function doesn't work

查看:79
本文介绍了Meteor.setTimeout 函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上浏览了很长时间,试图找出我的代码有什么问题,但找不到有效的答案.

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

我只是想为触发 GoogleMap 函数设置 2 秒延迟,但它不起作用.我已经尝试了很多不同的事情,比如向我的函数声明一个 var 然后匿名触发 setTimeout 函数等等......但没有运气......我没有从控制台收到错误所以我觉得我的代码写得很好,Meteor 文档没有提供关于 setTimeout 函数的太多信息.

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.

这也行不通:

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

推荐答案

将 setTimeout 中的代码放到这样的外部函数中:

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

然后调用setTimeout里面的函数不带括号,像这样:

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

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

如果您使用括号调用该函数,该函数将立即执行,而不会在 timeOut 中指定延迟.

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

这篇关于Meteor.setTimeout 函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆