使用setInterval更新按钮上的文本

时间:2015-08-06 06:00:39

标签: javascript jquery ajax laravel

我正在尝试使用ID fixed-button获取我的按钮上的文字,以便使用此定期更新:

<script type="text/javascript">
    $(function()
    {
        $('#fixed-button').setInterval(function() {
            $.getJSON("{{ url('schedulizer/cart') }}", function(data) {
                $(this).text(data.quantity);
            });
        }, 5000);
    });
</script>

但是,我收到的JS错误是:

Uncaught TypeError: $(...).setInterval is not a function

问题很明显 - this属性没有.setInterval功能。所以问题是 - 如何在常规时间段内使用AJAX在此按钮元素上设置文本?

1 个答案:

答案 0 :(得分:2)

  

setInterval不是jQuery函数,它是一个javascript函数。

<script type="text/javascript">
    $(function()
    {
        var $this=$('#fixed-button');
        window.setInterval(function() {
            $.getJSON("{{ url('schedulizer/cart') }}", function(data) {
                $this.text(data.quantity);
            });
        }, 5000);
    });
</script>

DOC: setInterval