Meteor-timers

提供:Dev Guides
移動先:案内検索

流星-タイマー

Meteorは、独自の setTimeout および setInterval メソッドを提供します。 これらのメソッドは、すべてのグローバル変数が正しい値を持っていることを確認するために使用されます。 通常のJavaScript setTimout および setInterval のように機能します。

タイムアウト

これは Meteor.setTimeout の例です。

Meteor.setTimeout(function() {
   console.log("Timeout called after three seconds...");
}, 3000);

コンソールで、アプリが起動するとタイムアウト関数が呼び出されることがわかります。

流星タイムアウト

間隔

次の例は、間隔を設定およびクリアする方法を示しています。

meteorAppl

<head>
   <title>meteorApp</title>
</head>

<body>
   <div>
      {{> myTemplate}}
   </div>
</body>

<template name = "myTemplate">
   <button>CLEAR</button>
</template>

間隔呼び出しのたびに更新される初期 counter 変数を設定します。

meteorApp.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 変数を3秒ごとに記録します。 CLEAR ボタンをクリックして、これを停止できます。 これにより、 clearInterval メソッドが呼び出されます。

流星間隔