Reputation:
I have 10 Question with multiple answers. I want to generate new question in after every 2 minute. And also show the time to the User with each Question . How I can achieve this goal ?
I can't Display Time
<div>
<h2> Time left : @*Here I want to show Time*@ </h2>
</div>
<div class="row">
@if (id != 0)
{
@*This is Question Component & Here I pass the Question id *@
<QuestionCard Id="@id.ToString()"></QuestionCard>
}
else
{
<MatH1>Loading . . . . .</MatH1>
}
</div>
@code {
System.Timers.Timer aTimer = new System.Timers.Timer();
private int id { get; set; } = 0;
protected override async Task OnInitializedAsync()
{
await Task.Run(() => Start());
}
void Start()
{
//aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
id = 2; //I Just pass manually Question id
aTimer.Interval = 5000;
aTimer.Enabled = true;
aTimer.Start();
}
}
I am totally new on Blazor. If have any mistake please forgive.
Upvotes: 2
Views: 3170
Reputation:
I did this to show Time
<p> @TimeLeft </p>
@code{
TimeSpan TimeLeft = new TimeSpan(0, 0, 15);
string displayText = "";
bool show=false;
void Start()
{
Task.Delay(1000);
displayText = "Start Time";
show = true;
Timer();
}
async Task Timer()
{
while (TimeLeft > new TimeSpan())
{
await Task.Delay(1000);
TimeLeft = TimeLeft.Subtract(new TimeSpan(0, 0, 1));
StateHasChanged();
}
await AfterTime();
StateHasChanged();
}
Task AfterTime()
{
displayText = "Time Expire";
TimeLeft = new TimeSpan(0, 0, 15);
return Task.CompletedTask;
}
}
Upvotes: 5