不活动时停止 UpdatePanel

发布于 2024-07-17 07:53:00 字数 330 浏览 2 评论 0原文

您好,我想执行以下操作:

当浏览器处于非活动状态时,使用 JavaScript 停止更新面板“更新”,并在浏览器再次活动后重新启动。 我正在查看:Inacivity with JavaScript。 我希望我能用它来完成我想要的事情; 有人能指出我正确的方向吗? 非常感谢!

[编辑] 我正在使用计时器进行更新,因此如果我可以以某种方式从 JavaScript 禁用计时器,我想我可以做到这一点。

Hi I would like to do the following:

Use JavaScript to stop the update panel from "updating" when the browser is inactive, and restart once it’s active again. I was looking at: Inacivity with JavaScript. I was hoping that I could use this to accomplish what I want; can someone point me in the right direction? Thank you very much!

[EDIT]
I am using a Timer for the updates so if I can somehow disable the Timer from JavaScript I think I can do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诺曦 2024-07-24 07:53:00

我建议阅读这本书:http://oreilly.com/catalog/9780596527471/。 它告诉您需要了解的有关 ASP.NET AJAX UpdatePanel 控件的所有信息。 有关 API 文档,请访问:http://msdn.microsoft.com/nb-no/library/ bb311028(en-us).aspx

I recommend reading this book: http://oreilly.com/catalog/9780596527471/. It tells you everything you need to know about the ASP.NET AJAX UpdatePanel Control. For API documentation please visit: http://msdn.microsoft.com/nb-no/library/bb311028(en-us).aspx.

放手` 2024-07-24 07:53:00

如何利用当前窗口对象的“mousemove”事件?
如果鼠标已移动,请将计时器设置为 1 分钟左右,然后停用事件。
使用 setInterval() 重复此循环。

如果用户小睡了一段时间,这也会有所帮助。

What's about taking advantage of current window object's 'mousemove' event?
If mouse has been moved, set the timer for 1 min or so, and deactivate event.
Repeat this cycle using setInterval().

It would also help, if the user had snoozed for a while.

老娘不死你永远是小三 2024-07-24 07:53:00
window.blur = function() { stopTimer() }
window.focus = function() { startTimer() }

浏览器兼容性可能适用。 希望这可以帮助。

window.blur = function() { stopTimer() }
window.focus = function() { startTimer() }

browser compatibility may apply. Hope this helps.

分開簡單 2024-07-24 07:53:00

我在这里找到了一个潜在的解决方案: http://www.thefutureoftheweb.com/blog /detect-browser-window-focus

我现在只有 IE8 可以测试,它有几个问题。 首先,单击页面上的各个元素将触发 focusin 事件,因此您需要一种方法来检查计时器是否已经在运行。 这并不太难。 当您点击时,ie8 还会触发 focusout 事件,因此我们必须延迟实际的计时器停止,以确保窗口实际上没有焦点。 幸运的是,当您点击页面时,ie8 似乎总是在 focusout 之后立即触发 focusin,因此我们可以取消 focusin 上待处理的计时器关闭> 处理程序。

我认为这应该非常接近一个可行的解决方案。 (改编自链接)

// indicates if your refresh timer is running
var updatePanelTimerRunning = true;

// timer id for shutdown procedure
var killTimerID = -1;

function stopTimer(e) {
    // prepare to shutdown
    if (updatePanelTimerRunning && killTimerID == -1) {
        killTimerID = setTimeout(timerShutdown, 1000);
    }
}

function startTimer(e) {

    // cancel pending timer shutdown
    clearTimeout(killTimerID);
    killTimerID = -1;

    if (!updatePanelTimerRunning) {
        // TODO start the update panel refresh timer here
        updatePanelTimerRunning = true;
    }
}

function timerShutdown() {
    // TODO do actual update panel timer stopping here.
    updatePanelTimerRunning = false;
}


if (/*@cc_on ! @*/ false) { // check for Internet Explorer

    document.attachEvent('onfocusin', startTimer);
    document.attachEvent('onfocusout', stopTimer);
}
else {
    // window.onfocus = stopTimer;
    // window.onblur = startTimer;

    window.addEventListener('focus', startTimer, false);
    window.addEventListener('blur', stopTimer, false);
}

I found a potential solution here: http://www.thefutureoftheweb.com/blog/detect-browser-window-focus

i only have IE8 to test with right now, which has a couple issues. first, clicking around the various elements on a page will fire the focusin event, so you need a way to check if the timer is already running. that's not too hard. ie8 also fires the focusout event while you're clicking around, so we have to delay the actual timer stoppage to make sure the window doesn't actually have focus. fortunately ie8 appears to always fire focusin right after focusout while you're clicking on the page, so we can cancel a pending timer shutdown on the focusin handler.

i think this should be pretty close to a working solution. (adapted from the link)

// indicates if your refresh timer is running
var updatePanelTimerRunning = true;

// timer id for shutdown procedure
var killTimerID = -1;

function stopTimer(e) {
    // prepare to shutdown
    if (updatePanelTimerRunning && killTimerID == -1) {
        killTimerID = setTimeout(timerShutdown, 1000);
    }
}

function startTimer(e) {

    // cancel pending timer shutdown
    clearTimeout(killTimerID);
    killTimerID = -1;

    if (!updatePanelTimerRunning) {
        // TODO start the update panel refresh timer here
        updatePanelTimerRunning = true;
    }
}

function timerShutdown() {
    // TODO do actual update panel timer stopping here.
    updatePanelTimerRunning = false;
}


if (/*@cc_on ! @*/ false) { // check for Internet Explorer

    document.attachEvent('onfocusin', startTimer);
    document.attachEvent('onfocusout', stopTimer);
}
else {
    // window.onfocus = stopTimer;
    // window.onblur = startTimer;

    window.addEventListener('focus', startTimer, false);
    window.addEventListener('blur', stopTimer, false);
}
千柳 2024-07-24 07:53:00

在 head 事件中添加此脚本,

<script type ="text/javascript">
    var timerEnabled = true;

    function ToggleTimer(btn, timerID)
    {
        // Toggle the timer enabled state
        timerEnabled = !timerEnabled;

        // Get a reference to the Timer
        var timer = $find(timerID);
        if (timerEnabled)
        { 
            btn.value = 'Pause';
            // Start timer
            timer._startTimer();
        }
        else
        {
            btn.value = 'Resume'; 
            // Stop timer
            timer._stopTimer();
        }            
    }
</script>

在更新面板内添加一个 asp.net 标签控件用于测试

<asp:Label Id="Label1" Text="" runat="server" />

在更新面板外添加一个 html 按钮

<input type="button" id="btntimercontroller" value="Pause" onclick="return ToggleTimer(btntimercontroller, '<%= ajaxtimer.ClientID%>');" />

以检查计时器是否停止在 aspx.cs 文件中添加此代码

protected void Page_Load(object sender, EventArgs e)
{       
    Label1.Text = DateTime.Now.ToLongTimeString();    
}

只需省略 (") 标记在脚本中,因为它会导致错误......

add this script inside the head event,

<script type ="text/javascript">
    var timerEnabled = true;

    function ToggleTimer(btn, timerID)
    {
        // Toggle the timer enabled state
        timerEnabled = !timerEnabled;

        // Get a reference to the Timer
        var timer = $find(timerID);
        if (timerEnabled)
        { 
            btn.value = 'Pause';
            // Start timer
            timer._startTimer();
        }
        else
        {
            btn.value = 'Resume'; 
            // Stop timer
            timer._stopTimer();
        }            
    }
</script>

add an asp.net label control inside the update panel for testing

<asp:Label Id="Label1" Text="" runat="server" />

add an html button outside the update pannel

<input type="button" id="btntimercontroller" value="Pause" onclick="return ToggleTimer(btntimercontroller, '<%= ajaxtimer.ClientID%>');" />

to check if the timer stops add this code in aspx.cs file

protected void Page_Load(object sender, EventArgs e)
{       
    Label1.Text = DateTime.Now.ToLongTimeString();    
}

Just omit the (") mark in the Script because it will cause an error...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文