【问题标题】:How to stop blinking when particular div is reload重新加载特定div时如何停止闪烁
【发布时间】:2019-09-11 11:12:57
【问题描述】:

我已经使用 setTimeout 函数重新加载我的通知下拉菜单,现在它运行良好,但是我的通知下拉菜单在重新加载时闪烁,请提供任何解决方案来停止闪烁。

这里附上我已经用过的。

$(document).ready(function(){
    notification();
  });

function notification()
{
    setTimeout(function()
    {
        $('#notification_ico').load('<?php echo base_url();?>/Filemanagement/noti_refresh');
        notification();
    }, 3000);
}
function abc() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
        document.getElementById("notification_ico").innerHTML = this.responseText;
        }
        };
        xhttp.open("GET", "<?php echo base_url();?>/Filemanagement/noti_refresh", true);
        xhttp.send();
        }

我的页面不工作

【问题讨论】:

  • 您在超时内重新加载后再次调用 notification() 函数,这导致通知函数一次又一次的递归调用并导致闪烁
  • 我需要显示最近的通知,这就是为什么我在 setTimeout 函数中调用通知函数,所以它每 3 秒调用一次通知函数
  • 你应该使用setInterval而不是setTimeout

标签: javascript jquery html codeigniter


【解决方案1】:

这里的问题是每次调用通知时都会调用一个新的 setTimeout 而不会杀死前一个,因此最终会并行执行多个。

你应该改用 setInterval :

$(document).ready(function () {
  setInterval(function () {
    $('#notification_ico').load('<?php echo base_url();>/Filemanagement/noti_refresh');
  }, 1000);
});

你可以试试:https://stackblitz.com/edit/js-chts5e

【讨论】:

  • 不是自己刷新的
猜你喜欢
  • 2017-07-19
  • 2013-05-07
  • 2012-06-19
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
相关资源
最近更新 更多