Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Meteor.setTimeout函数不';行不通_Javascript_Meteor_Settimeout - Fatal编程技术网

Javascript Meteor.setTimeout函数不';行不通

Javascript Meteor.setTimeout函数不';行不通,javascript,meteor,settimeout,Javascript,Meteor,Settimeout,我在互联网上搜索了很长一段时间,试图找出我的代码出了什么问题,但找不到有效的答案 Template.map.onCreated(function() { Meteor.setTimeout(function() { GoogleMaps.ready('exampleMap', function(map) { var marker = new google.maps.Marker({ position: map.options.center,

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

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 docs没有提供关于
setTimeout
函数的太多信息

这也不管用:

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内的代码转换为如下外部函数:

function prepareMap() {
      GoogleMaps.ready('exampleMap', function(map) {
        var marker = new google.maps.Marker({
          position: map.options.center,
          map: map.instance
        });
      });
    }
 }
Template.map.onCreated(function() {
   setTimeout(prepareMap, 2000);
});
并调用setTimeout中的函数,不带括号,如下所示:

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

如果使用括号调用函数,函数将立即执行,而不会出现超时中指定的延迟

在没有设置超时的情况下,任何东西都可以工作吗?什么是func和delay?我认为您粘贴了Meteor.setTimeout的语法,但没有删除默认参数。试试Meteor.setTimeout(函数(){…},2000)附言。如果你想在dom呈现后做点什么,你也可以看看@Kyll。是的,没有setTimeout函数,一切都可以正常工作。不要将就绪函数放在map.onCreated中,而是尝试将
tracker.afterFlush()
放在
Template.map.onRendered中。你可以使用tracker.afterFlush
就像Meteor.setTimeout一样,只是它不需要延迟参数。好吧,phooey,我必须在调试器中查看它。这绝对不是javascript中匿名函数的工作方式。函数定义仅在包含在IIFE(立即调用的函数表达式)中时才立即执行,如
(Function(){…})(
请注意末尾的额外括号表示函数的执行