theGreenCabbage
theGreenCabbage

Reputation: 4845

Updating text on a button using setInterval

I am trying to get the text on my button with the id fixed-button to update in a regular period using this:

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

However, the JS error I received is:

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

The issue is obvious - the this property has no .setInterval function. So the question is - how do I set the text on this button element using AJAX on a regular period of time?

Upvotes: 0

Views: 201

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

setInterval is not a jQuery function, it's a javascript function.

<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

Upvotes: 2

Related Questions