一段时间后生成回发

Generate a postback after some time has passed

本文关键字:后生 一段时间      更新时间:2023-09-26
一段时间

后是否可以生成回发?假设:我有一些用户必须在文本框中键入的文本。如果他在接下来的 1 分钟内没有键入任何内容,则应刷新页面,显示警告或其他内容

这篇文章应该会有所帮助: http://www.codeproject.com/Articles/12293/Auto-postback-in-ASP-NET

但是,您必须集成自己的有关回发的业务逻辑,或者不集成,因为您未能包含源代码。 但是,此示例包含一个StopTheClock()方法,因此您可能希望将某种"OnChange"事件附加到文本框,该事件在值不为空时停止时钟。

另一种方法是在每个刻度之后(在本例中为 1 秒)和回发 (form.submit) 之前检查文本框的值,然后在 textbox.value 不为空时简单地停止计时器。

<script language="JavaScript" type="text/javascript">
<!--
// Script to generate an automatic postBack to the server
var secs
var timerID = null
var timerRunning = false
var delay = 1000
function InitializeTimer()
{
    // Set the length of the timer,
     // in seconds. Your choise
    secs = 5
    StopTheClock()
    StartTheTimer()
}
function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}
function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        //Generate a Postback to the server
        document.forms[0].submit()  
    }
    else
    {
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
//-->
</script>

<body onload="InitializeTimer()">
<form id="form1" runat="server">
 <div>
       The time is:   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>

我强烈建议不要采用这样的方法。您所需要的只是通知用户或修改页面的一部分。完整的回发真的是一种浪费。相反,我建议使用Ajax。也许使用 javascript 添加一个计时器,监听所需的事件并根据需要发出 ajax 请求。