I'm having a countdown timer like these
var leave =<%=seconds %>;
CounterTimer();
var interv=setInterval("CounterTimer()",1000);
function CounterTimer() {
try {
if (leave>0)
{
var day = Math.floor(leave / ( 60 * 60 * 24))
var hour = Math.floor(leave / 3600) - (day * 24)
var minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60)
var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60)
hour=hour<10 ? "0" + hour : hour;
minute=minute<10 ? "0" + minute : minute;
second=second<10 ? "0" + second : second;
var remain=day + " days "+hour + ":"+minute+":"+second;
leave = leave - 1;
document.getElementById("<%=lblRemain.ClientID %>").innerText=remain;
}
else
{
try{
document.getElementById("<%=lblRemain.ClientID %>").innerText="Auction closed";}
catch(err) { //alert(err.Message);
}
}
}
catch(err)
{ alert(err.Message);}
}
And I'm setting/putting the value of the leave variable from c#
seconds = (ClosingOn - DateTime.Now).TotalSeconds;
On the webpage I have a UpdatePanel showing auction information and this UpdatePanel is updated every 5 second. During the countdown I would like to be able to change the remaining time in the timer event
protected void Timer1_Tick(object sender, EventArgs e)
{
ClosingOn.AddMinutes(5);
seconds = (ClosingOn - DateTime.Now).TotalSeconds;
}
But this change doesn't affect the leave variable in the JavaScript :(
Can you see a way out of this?
I'm on a very slow interent connection so having a updatepanel updating every second is not an option.
That's because this expression var leave =<%=seconds %>;
evaluated only once, actually on page rendering. Instead of evaluating leave value with <%= seconds %> expression, add HiddenField into UpdatePanel where Timer control placed. In javascript you can get this field's value and parse it to int. And in Timer1_Tick you can assign on that hidden field new value.
Or if you looking for a quick workaround you can add at the end of Timer1_Tick method following code:
ScriptManager.RegisterStartupScript(Timer1, typeof(Timer), Guid.NewGuid().ToString(), string.Format("leave = {0}", seconds), true);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.