码迷,mamicode.com
首页 > 其他好文 > 详细

Meteor计时器

时间:2017-08-18 15:53:17      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:接下来   client   自己   结果   body   .com   javascrip   called   调用   

Meteor有提供它自己的setTimeout和setInterval方法。这些方法被用于确保所有全局变量都具有正确的值。它们就像普通 JavaScript 中的setTimeout 和 setInterval 一样工作。

Timeout - 超时

Meteor.setTimeout 的例子。
Meteor.setTimeout(function(){
   console.log("Timeout called after three seconds..."); }, 3000);
当应用程序已经开始我们可以在超时函数调用(启动 3 秒后调用),在控制台中看到下面的输出结果。
技术分享
技术分享

Interval

接下来的例子显示如何设置和清除interval。

meteorApp/client/app.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <div>
      {{> myTemplate}}
   </div>
</body>
 
<template name = "myTemplate">
   <button>CLEAR</button>
</template>
我们将设置将 counter 的初始变量在每次调用后更新。

meteorApp/client/app.js

if (Meteor.isClient) {

   var counter = 0;

   var myInterval = Meteor.setInterval(function(){
      counter ++
      console.log("Interval called " + counter + " times...");
   }, 3000);

   Template.myTemplate.events({
      ‘click button‘: function(){
         Meteor.clearInterval(myInterval);
         console.log(‘Interval cleared...‘)
      }
   });                                                    

} 

控制台将每三秒钟记录更新计数器- counter 变量。我们可以通过点击 CLEAR 按钮清除。这将调用 clearInterval 方法。
技术分享

技术分享

Meteor计时器

标签:接下来   client   自己   结果   body   .com   javascrip   called   调用   

原文地址:http://www.cnblogs.com/h2zZhou/p/7390002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!