【问题标题】:Auto Refresh div using JavaScript使用 JavaScript 自动刷新 div
【发布时间】:2013-11-25 13:50:32
【问题描述】:

我有一个计量器,每次单击按钮时都会刷新并获取随机数据。

我的问题是如何在不点击按钮的情况下执行此操作,我希望它每 5 秒自动刷新一次。

 <div id="gg1" class="gauge">
</div>
<a href="#" id="gg1_refresh" class="button grey">Random Refresh</a>
<script>
    var gg1 = new JustGage({
        id: "gg1",
        donut: 1,
        title: "BARTOW",
        titleFontColor: "#000000",
        value: 95.7,
        min: 0,
        max: 100,           
        labelFontColor: "#000000",
        humanFriendlyDecimal: 1,
        decimals: 1,
        symbol: "%",
        refreshAnimationType: "bounce",
        gaugeWidthScale: 0.6,
        customSectors: [{
            color: "#ff0000",
            lo: 0,
            hi: 79
        }, {
            color: "#ffa500",
            lo: 80,
            hi: 89
        }, {
            color: "#1eae1e",
            lo: 90,
            hi: 100
        }],
        counter: true
    });
    $(document).ready(function () {
        $('#gg1_refresh').bind('click', function () {
            gg1.refresh(getRandomInt(0, 100));
            return false;

        });
    });
</script>

【问题讨论】:

标签: javascript jquery raphael graphael justgage


【解决方案1】:

您需要使用setInterval 每 5 秒调用一次函数。

您需要创建一个包含代码的函数

gg1.refresh(getRandomInt(0, 100));

然后在你的 click 事件处理程序中使用 setInterval 函数来调用你上面创建的函数。

【讨论】:

  • 我是 JavaScript 新手。我该怎么做?
  • 页面上有一个示例,可以让您使用javascript函数。我不会为您编写代码,因为我认为这不是学习的方式。我在回答中包含了关于您需要做什么的描述
  • @Apollo google jquery setInterval 你会找到必要的信息
  • @thenewseattle setInterval 不是 jQuery 函数,它是一个不需要任何库的标准 javascript 函数。
  • @SandeepBansal 没错。但 OP 已经在他的问题中使用 jQuery。
【解决方案2】:

查看以下内容,根据@thenewseattle 的评论进行编辑。

<script type="text/javascript">
    function refreshDiv() {
       gg1.refresh(getRandomInt(0, 100));
    }
    $(document).ready(function () { setInterval(refreshDiv, 5000); });
</script>

【讨论】:

  • -1 OP 要求 setInterval。没有设置超时。
  • 在我看来你不应该改变它。 setTimeout 更好/更安全,因为setInterval 不分青红皂白地运行并且不在乎您的脚本是否已完成,这意味着您调用该函数的速度可能比它完成的速度快。使用setTimeout() 会好得多,但在使用clearTimeout() 重复使用之前,您应该清除它
  • 是的,我认为 setTimeout() 如果你基于任何非瞬时的更新会更好,因为你不想堆积一堆刷新调用t 完成。但是,OP 的示例是瞬时刷新,所以我认为在这种情况下它是安全的。无论哪种方式,在我看来,setTimeout() 肯定也有一个论据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 2013-03-11
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多