特定のニーズに応じてこれを行う方法はいくつかありますが、多くのReactive Timerパッケージのうち1つを使用したい場合もあります。
ここでは、Meteor API(パッケージなし)のみを使用する1つの実例を示します。注意していただきたいのは、実際にローダーサークルのアニメーションを組み込んでいないということです。
テンプレート定義
<template name="loader">
<h1>Loading...({{loadingPercentage}})</h1>
</template>
テンプレートロジック
Template.loader.onCreated(function() {
// used to update loader circle and triggering method invocation
this.elapsedTime = new ReactiveVar(0);
// handle for the timer
var timerHandle;
// starts a timer that runs every second that can be paused or stopped later
const startTimer =() => {
timerHandle = Meteor.setInterval(() => {
this.elapsedTime.set(this.elapsedTime.get() + 1);
}, 1000);
};
// pauses/stops the timer
const pauseTimer =() => {
Meteor.clearInterval(timerHandle);
};
// let's first start our timer
startTimer();
// runs every second and triggers methods as needed
this.autorun(() => {
const elapsedTime = this.elapsedTime.get();
// call methods based upon how much time has elapsed
if (elapsedTime === 5) {
pauseTimer();
// call method and restart the timer once it completes
Meteor.call('methodOne', function(error, result) {
// do stuff with the result
startTimer();
});
} else if (elapsedTime === 9) {
pauseTimer();
// call method and restart the timer once it completes
Meteor.call('methodOne', function(error, result) {
// do stuff with the result
// DO NOT RESTART TIMER!
});
}
});
});
Template.loader.helpers({
// helper used to show elapsed time on the page
loadingPercentage: function() {
return Template.instance().elapsedTime.get();
},
});
ご質問があれば私に教えてください。
私はあなたの答えを感謝し、シンプルで理解しやすいようです。私はすでにこれを理解しており、私の方法はあなたが説明したものと非常に似ています。私は私のコードを追加し、以下に答えます。 –