如何使用j查询功能设置多个计时器?

时间:2017-05-13 05:42:35

标签: jquery

在我的应用程序中,我想从数据库加载一些数据,并尝试在我的代码中设置多个计时器。我认为我的代码有一些错误。

我的代码

  function stopclock(e)
    {

    // e = 30*30*/8*1* 

     var nn =e.split('/');
     var ss =nn[0];
     var tt =nn[1];

     var jj1= ss.split('*');
     var uu1 = tt.split('*');

    var count = jj1.length;


        for(k = 0; k < count-1; k++)
         {
             var table_id33 = uu1[k];

             var seconds = 60 * jj1[k];

            secondPassed(table_id33,seconds);

        }
     }

       function secondPassed(table_id44,seconds) 
            {

             var rr ='time'+table_id44;

                var abs_seconds = Math.abs(seconds);
                var is_negative = seconds < 0;
                var minutes = Math.round((abs_seconds - 30)/60);
                var remainingSeconds = abs_seconds % 60; 
                if (remainingSeconds < 10) 
                {
                    remainingSeconds = "0" + remainingSeconds;  
                }
                if (minutes < 10) 
                {
                    minutes = "0" + minutes;  
                }
                document.getElementById('time'+table_id44).innerHTML = (is_negative ? '-' : '') + minutes + ":" + remainingSeconds;
                seconds--;

                if (!is_negative)
                 {
                    if (seconds == 0)
                    {
                        $('#time'+table_id44).css('color','red');
                    }
                }
            }
            var countdownTimer = setInterval(secondPassed, 1000);

当我把我的计时器显示在我的div中时显示但是它没有工作。请帮助。

2 个答案:

答案 0 :(得分:1)

  

选中此代码段 - 点击 <号码

   

 function timer(selector) {
        var self = $(selector);
        var sec = parseInt(self.find('span.timeout').text());

        var interval = setInterval(function() {
            sec--;
            if (sec >= 0) {
                self.find('span.timeout').text(sec);
            } else {
                clearInterval(interval);
            }
        }, 1000);
    }


    $("body").on('click', '.element', function() {
        timer(this);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   

 <div class="element" id="el1"><span class="timeout">10</span></div>
    <div class="element" id="el2">
    <span class="timeout">10</span>
    </div>

答案 1 :(得分:0)

定时器功能

        function countdown1( elementName, minutes, seconds)
        {

            var element, endTime, hours, mins, msLeft, time;

            function twoDigits(n)
            {
                return (n <= 9 ? "0" + n : n);
            }

            function updateTimer()
            {
                msLeft = endTime - (+new Date);
                if ( msLeft < 1000 ) 
                {
                    element.innerHTML = "countdown's over!";
                } else {
                    time = new Date( msLeft );
                    hours = time.getUTCHours();
                    mins = time.getUTCMinutes();
                    element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );
                    setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
                }
            }

            element = document.getElementById( elementName );
            endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
            updateTimer();  
        }