简体   繁体   English

javascript倒计时计时器闪烁多次调用

[英]javascript count down timer flickering on calling more than once

function countdown(element, minutes, seconds) {
    // set time for the particular countdown
    var time = minutes*60 + seconds;
    var interval = setInterval(function() {
        var el = document.getElementById(element);
        // if the time is 0 then end the counter

        if(time == 0) {
            //el.innerHTML = "countdown's over!";
         // document.getElementById("timer").style.visibility="hidden";
            clearInterval(interval);
            return;
        }
        var hour=Math.floor( time / (60*60) );
        if (hour < 10) hour = "0" + hour;
        var minutes = 0;
        if(time>=60 && hour>0)
            minutes=Math.floor( (time / 60 )-60);
        else{
            minutes=Math.floor( (time / 60 ));
        }
        if (minutes < 10) minutes = "0" + minutes;
        var seconds = time % 60;
        if (seconds < 10) seconds = "0" + seconds; 
      //  var text = hour+":"+minutes; //+ ':' + seconds;
        var text = minutes; //+ ':' + seconds;

        el.innerHTML = text;
        time--;
    }, 1000);
}

when i am calling the method 2wice, its creating flickering effect. 当我调用2wice方法时,会产生闪烁效果。 ie countdown(element, 50, 0); countdown(element, 50, 0); it count downs but if i call it again ie countdown(element, 35, 0); 它倒数,但如果我再次调用它,即countdown(element, 35, 0); it is flicks showing both the countddowns 它显示了两个倒计时

You need to cancel the interval when the plugin initializes if you are going to call it on the same element. 如果要在同一元素上调用插件,则需要取消插件初始化的时间间隔。 Otherwise you are running two intervals at once. 否则,您一次运行两个时间间隔。

I'd suggest returning the interval from your function and allowing yourself to pass that interval in as a parameter. 我建议从您的函数返回间隔,并允许您自己将该间隔作为参数传递。 That way you can always cancel the interval before starting it (in case there is already one going). 这样,您始终可以在开始间隔之前取消该间隔(以防万一)。 If you pass null for the interval var, you can still run clearInterval() without throwing errors. 如果为间隔var传递null ,则仍可以运行clearInterval()而不会clearInterval()错误。

For example: 例如:

function countdown(element, minutes, seconds, interval) {
    // set time for the particular countdown
    var time = minutes*60 + seconds;
    clearInterval(interval);
    return setInterval(function() {
...

Your first call could be: 您的第一个电话可能是:

var savedInterval = countdown('some-ele-id', 1, 1, null);

And your second call could be: 您的第二个电话可能是:

var interval = countdown('some-ele-id', 1, 1, savedInterval);

Another solution would be to save the interval as a global variable and cancel it without passing it as a parameter inside your plugin. 另一种解决方案是将时间间隔另存为全局变量,然后取消它,而无需将其作为参数传递到插件内部。

An alternative to avoid modify your code is: 避免修改代码的另一种方法是:

var interval;
...
function countdown(element, minutes, seconds) {
    // set time for the particular countdown
    var time = minutes*60 + seconds;
    interval = setInterval(function() {
...

And in your second call: 在第二个电话中:

clearInterval(interval);
countdown(element, 35, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM