Nativescript getViewById不更新以进行倒计时 [英] Nativescript getViewById not updating for count down
问题描述
So, i am using the timer that i asked about in this question as a count down: How to stop timer.setinterval with a tap event or leaving page in nativescript
计数器工作正常,而我一直在使用console.log来检查它是否工作,是的,但是我想使它出现在xml中,供该应用程序的用户查看.但是计数不会在xml中更新.
The counter works, all this while i have been using console.log to check if it works and yes it does but i want to make it appear in the xml for the user of that app to see. But the count does not update in the xml.
以下是该视图的代码:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" navigatedTo="navigatedTo" navigatingFrom="onNavigatingFrom" actionBarHidden="true">
<DockLayout orientation="horizontal" horizontalAlignment="center" backgroundColor="black" color="white">
<Label text="Time remaining: " dock="left" margin="5" />
<Label id="timeCount" text="" loaded="pageLoaded"></Label>
</DockLayout>
</Page>
以及后面的js文件:
var time;
var timeKeep = vm.time;
var count = 0;
function timeCountDown () {
time = vm.time--; //this is the value that i need
console.log("i is " + time);
}
countId = timer.setInterval(() => {
timeCountDown();
count += 1;
if (count === timeKeep) {
timer.clearInterval(countId);
dialogs.alert({
title: "Time Up!",
message: "You did not finish the test in time.",
okButtonText: "OK"
});
aemnavigation.goResults(vm.correct);
}
}, 1000);
//the two lines below is how i get it to show on the xml
TimeCountScore = page.getViewById("timeCount");
TimeCountScore.text = time;
时间值到达视图,但是时间值递减时不会更新.我console.log(time)以确保它仍然很重要.
The value of time gets to the view but it's not updated has the value counts down. I console.log(time) to make sure that it still counts.
推荐答案
您应该data binding
继承Observable类,当属性更改时,它也会同时更新XML.对于您的情况,如果您仍想继续练习,可以将label.text
放在时间间隔内以进行更新:
You should you data binding
that inherits the Observable class, when a property is changed it will update the XML also. For your case, if you still want to keep your practice you can put the label.text
inside the interval to get it updated:
TimeCountScore = page.getViewById("timeCount");
countId = timer.setInterval(() => {
TimeCountScore.text = time;
}, 1000);
这篇关于Nativescript getViewById不更新以进行倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!