ดาวตก - ตัวจับเวลา

Meteor เสนอของตัวเอง setTimeout และ setIntervalวิธีการ วิธีการเหล่านี้ใช้เพื่อให้แน่ใจว่าตัวแปรส่วนกลางทั้งหมดมีค่าที่ถูกต้อง ทำงานเหมือน JavaScript ทั่วไปsetTimout และ setInterval.

หมดเวลา

นี่คือ Meteor.setTimeout ตัวอย่าง.

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

เราจะเห็นในคอนโซลว่าฟังก์ชันหมดเวลาถูกเรียกใช้เมื่อแอปเริ่มทำงานแล้ว

ช่วงเวลา

ตัวอย่างต่อไปนี้แสดงวิธีการตั้งค่าและล้างช่วงเวลา

eorApp.html

<head>
   <title>meteorApp</title>
</head>
 
<body>
   <div>
      {{> myTemplate}}
   </div>
</body>
 
<template name = "myTemplate">
   <button>CLEAR</button>
</template>

เราจะตั้งค่าเริ่มต้น counter ตัวแปรที่จะได้รับการอัปเดตหลังจากการเรียกทุกช่วงเวลา

eorApp.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 วิธี.