【问题标题】:jQuery countdown function in IEIE中的jQuery倒计时功能
【发布时间】:2011-11-02 11:51:38
【问题描述】:

我在使用 IE8 及以下版本时遇到了一个奇怪的问题,我的倒计时只应该触发一次,而这在现代浏览器中不会发生。有没有什么办法解决这一问题?谢谢!目标是在选择输入后开始倒计时。

代码:

$("input").focus (function counter() {
  $("input").unbind('focus', counter);
  var count = 60;
  countdown = setInterval(function(){
    $(".clock p").html(count);
    if (count == 0) {
      $(".while-ticking").fadeOut(1000);
      $(".countdown-finished").fadeIn(1000);
    }
    else {
        count--;
    }
  }, 1000);
});

【问题讨论】:

    标签: jquery internet-explorer countdown


    【解决方案1】:

    试试这个方法:

    $("input").focus(function() { counter(60) });
    
    function counter(count) {
        $("input").unbind('focus');
        setTimeout(function(){
            $(".clock p").html(count);
            if (count == 0) {
                $(".while-ticking").fadeOut(1000);
                $(".countdown-finished").fadeIn(1000);
            }
            else {
                counter(count--);
            }
        }, 1000);
    }
    

    编辑: 您需要为倒计时创建一个循环。所以我基本上做的是从 60 等待一秒开始,然后再次调用该函数,然后再调用 59.. 等等,当它为 0 时,它不再调用该函数,所以它停止了

    编辑: 我已经对其进行了以下测试,并且效果很好:)倒计时非常好

    <script type="text/javascript" src=".............../jquery-1.4.4.min.js"></script>
    <script type="text/javascript">
    
        $(document).ready(function() {
    
            $("input").focus(function() { counter(60) });
    
        });
    
        function counter(count) {
            $("input").unbind('focus');
            $(".clock p").html(count);
            setTimeout(function(){
                if (count == 0) {
                    $(".while-ticking").fadeOut(1000);
                    $(".countdown-finished").fadeIn(1000);
                }
                else {
                    console.log(count--);
                    counter(count--);
                }
            }, 1000);
        }
    
    </script>
    
    
    <input type="text" />
    
    <div class="clock">
        <p></p>
    </div>
    

    【讨论】:

    • 不,这行不通。此外,取消绑定焦点,计数器是必要的,因为否则 placeholderwer html 属性不起作用。不过还是谢谢。
    • The issue I'm having is that when more than one input field is selected it will fire again.也许我从一开始就不够清楚。
    • 它在 Firefox Opera 和 chrome 中运行良好,但在 IE 中仍然不行:/ 非常感谢。当我在 unbind 中添加计数器功能时,它会再次在所有浏览器中重新触发。 :(
    • 奇怪的是,你的整个代码似乎都依赖于console.log函数,没有它就无法工作。
    猜你喜欢
    • 2012-03-25
    • 2021-05-26
    • 1970-01-01
    • 2012-01-09
    • 2019-10-20
    • 2022-01-12
    • 1970-01-01
    • 2014-12-30
    • 2020-09-04
    相关资源
    最近更新 更多