jQuery 倒计时 jquery 事件

发布于 2024-09-30 22:40:37 字数 160 浏览 1 评论 0原文

我使用 jQuery 每 5 秒左右刷新一个 DIV 内容框,时间实际上是由最终用户控制的。 我想在页面上有一个倒计时器,它将根据设置的值进行倒计时,直到下一页刷新(或查询)。

目前,它可以是 5 秒的静态值,但我真的不知道如何在我的查询旁边运行倒计时。

非常感谢任何帮助。

I am using jQuery to refresh a DIV content box every 5 seconds or so, the time is actually controllable by the end user.
I want to have a countdown timer on the page which will count down until the next page refresh (or query) based on the value that is set.

For now, it can be a static value of 5 seconds but I really can't work out how to get a countdown running along side my query.

Any help is greatly appreciated.

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

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

发布评论

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

评论(4

清风夜微凉 2024-10-07 22:40:37

您可以使用 setInterval

var remaining = 5;

var myInterval = setInterval ( updateCountDown, 1000 );

function updateCountDown( )
{
  $("mydiv").text(remaining );
  remaining --;
  if (remaining == 0) {
    clearInterval(myInterval );
  }
}

You could use setInterval.

var remaining = 5;

var myInterval = setInterval ( updateCountDown, 1000 );

function updateCountDown( )
{
  $("mydiv").text(remaining );
  remaining --;
  if (remaining == 0) {
    clearInterval(myInterval );
  }
}
知足的幸福 2024-10-07 22:40:37

也许倒计时插件会有所帮助...
有一个 onExpiry 事件和 until 属性非常有用。

您可能会遇到的另一个问题是:js 不支持真正的多线程...

maybe the countdown plugin will help...
there's an onExpiry-event and the until-property is very helpful.

another problem you might stumble upon is: js does not support real multithreading...

怪我入戏太深 2024-10-07 22:40:37

编辑:
不确定我们是否理解您的意思:
您希望页面上显示计数器吗?

如果没有:

您可以在 JavaScript 中设置一个变量,例如

var TimeOutValue = 5000;

function OnTimeOut()
{
    // Add here your code for refreshing the DIV content
   window.setTimeout(OnTimeOut, TimeOutValue);
}

在输入字段的 textchanged 事件的处理程序中:例如

function InputTextChanged()
{
   TimeOutValue = document.getElementById("Id_of_Input_field").value;
}

Edit:
Not sure if we understood you:
Do you want a counter beeing displayed on the page?

If not:

You can set a variable in your JavaScript, e.g.

var TimeOutValue = 5000;

function OnTimeOut()
{
    // Add here your code for refreshing the DIV content
   window.setTimeout(OnTimeOut, TimeOutValue);
}

In the Handler for textchanged event of the input field: e.g.

function InputTextChanged()
{
   TimeOutValue = document.getElementById("Id_of_Input_field").value;
}
北笙凉宸 2024-10-07 22:40:37

嗯...我不久前也做过类似的事情。我使用了一个名为 jquery.timers 的 jQuery 插件:

这是我的代码:

    function timerTick() {
        counter--;
        $("#lblRepeat").html('searching again in ' + counter + ':');
        if (counter <= 0) {
            counter = 60;
            search();
        }
    }
    function chkActivate_click(ev) {
        var vr, t;
        t = $(this); 
        vr = t.is(':checked');
        if (vr) {
            t.everyTime(1000, 'busqueda', timerTick);
        } else {
            t.stopTime('busqueda');
        }
    }

Well... I did something similar a while ago. I used a plugin for jQuery called jquery.timers:

this is my code:

    function timerTick() {
        counter--;
        $("#lblRepeat").html('searching again in ' + counter + ':');
        if (counter <= 0) {
            counter = 60;
            search();
        }
    }
    function chkActivate_click(ev) {
        var vr, t;
        t = $(this); 
        vr = t.is(':checked');
        if (vr) {
            t.everyTime(1000, 'busqueda', timerTick);
        } else {
            t.stopTime('busqueda');
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文