简体   繁体   English

AJAX更新面板,将信息传递给JavaScript

[英]AJAX update panel, pass information to JavaScript

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# 我正在设置/输入c#中的离开变量的值

seconds = (ClosingOn - DateTime.Now).TotalSeconds;

On the webpage I have a UpdatePanel showing auction information and this UpdatePanel is updated every 5 second. 在网页上,我有一个显示拍卖信息的UpdatePanel,并且此UpdatePanel每5秒更新一次。 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 :( 但是此更改不会影响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. 我的Interent连接速度很慢,因此每秒都不能有一个updatepanel更新。

That's because this expression var leave =<%=seconds %>; 那是因为这个表达式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. 不用用<%= seconds%>表达式评估休假值,而是将HiddenField添加到放置了Timer控件的UpdatePanel中。 In javascript you can get this field's value and parse it to int. 在javascript中,您可以获取该字段的值并将其解析为int。 And in Timer1_Tick you can assign on that hidden field new value. 在Timer1_Tick中,您可以在该隐藏字段上分配新值。

Or if you looking for a quick workaround you can add at the end of Timer1_Tick method following code: 或者,如果您正在寻找一种快速的解决方法,则可以在Timer1_Tick方法的末尾添加以下代码:

ScriptManager.RegisterStartupScript(Timer1, typeof(Timer), Guid.NewGuid().ToString(), string.Format("leave = {0}", seconds), true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM