Javascript 将超时设置为LayoutTemplateMeteor/iron

Javascript 将超时设置为LayoutTemplateMeteor/iron,javascript,meteor,iron,Javascript,Meteor,Iron,我一直在努力为我的loadingTemplate设置3秒超时 使用代码bellow loadingTemplate进行渲染,但如我所料,它会在经过3秒后重定向到layoutTemplate 请查找以下我的代码和评论问题 我还将此版本部署到 谢谢你的帮助 Router.configure({ layoutTemplate: 'applayout', loadingTemplate: 'loading', waitOn: function () { var

我一直在努力为我的loadingTemplate设置3秒超时

使用代码bellow loadingTemplate进行渲染,但如我所料,它会在经过3秒后重定向到layoutTemplate

请查找以下我的代码和评论问题

我还将此版本部署到

谢谢你的帮助

Router.configure({
    layoutTemplate: 'applayout',
    loadingTemplate: 'loading',
    waitOn: function () {

        var isTimePassed = false;
        var clock = 3;

        var timeLeft = function() {
            if (clock > 0) {
                clock--;
                Session.set("time", clock);
                console.log(clock);

            } else {
                console.log("That's All Folks");
                //return true
                isTimePassed = true;
                Meteor.clearInterval(interval);
                console.log('is Time passed: '+ isTimePassed);
                return isTimePassed; // seems it is being ignored

            }
        };

        var interval = Meteor.setInterval(timeLeft, 1000);

        return {
            ready: function () {
                console.log('return ready: ' + isTimePassed);
                return isTimePassed; // keeps the loading page and does not redirect to applayout if changed to false, loadingTemplate is not loaded and
            }

        }

    }
});

从setInterval返回将不起任何作用。您需要使用反应变量并准备好返回该反应变量的值:

Router.configure({
    layoutTemplate: 'applayout',
    loadingTemplate: 'loading',
    waitOn: function () {
        Session.set('isTimePassed', false);
        var isTimePassed = false;
        var clock = 3;

        var timeLeft = function() {
            if (clock > 0) {
                clock--;
                Session.set("time", clock);
                console.log(clock);

            } else {
                console.log("That's All Folks");
                //return true
                isTimePassed = true;
                Meteor.clearInterval(interval);
                console.log('is Time passed: '+ isTimePassed);
                Session.set('isTimePassed', true);

            }
        };

        var interval = Meteor.setInterval(timeLeft, 1000);

        return {
            ready: function () {
                return Session.get('isTimePassed');
            }

        }

    }
});

但是,在您的问题中,这是否是您打算做的事情还不完全清楚。

经过几个小时的研究,我发现为加载模板设置自定义超时的最佳方法是不使用Router.configure。正确的方法是将对onBeforeAction函数的调用设置为my/route。因此,代码的结尾如下所示:

Router.configure({
layoutTemplate: 'appLayout',
loadingTemplate: 'loading'
});

Router.onBeforeAction(function(){
   if(!Session.get('templateLoaded'))
   {
    setTimeOut(1000);
    this.layout('loading');
    this.render('loading');
   }
  else
  {
    this.render('home');
    this.next();
  }
}, 
{
   only: ['home']

});

var setTimeOut = function (timeout) {
var self = this;
self._ready = false
self._dep = new Tracker.Dependency();

Meteor.setTimeout(function () {
    self._ready = true;
    self._dep.changed();
    if(Meteor.isClient){
        Session.set('templateLoaded', true); // set session variable to true so applayout Template will be rendered
    }

}, timeout);

  return function () {
    self._dep.depend();
    return function () {
        return self._ready;
   }
 }
};
未工作:(请检入控制台,您将看到正在重复调用,并且templateLayout在3秒钟后未呈现。我希望在3秒钟后看到的结果是templateLayout Appllayout,如所示。您还有其他建议吗?只返回一个
ready()
功能还不够。关于这方面有几个问题。似乎有一个更简单的方法。您可能也会发现有帮助。