xrcwrn
xrcwrn

Reputation: 5325

code is not calling in time interval

I want to call a jquery code in every 5 sec interval for that i am using following code but this code is not calling in repeatedly

My code is

 setInterval($.getJSON('friendshipRequestCount', function(data) {
        var cnt=data.totalFriendshipRequestCount;
        if(cnt>0){
            $('#f-request-count').css("display","block");
            $('#f-request-count').html(data.totalFriendshipRequestCount);
        }
        else{
            $('#f-request-count').css("display","none");
        }
    }),5000);

Please see what is the problem

Upvotes: 0

Views: 75

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113345

Pass a function as first parameter in setInterval. $.getJSON returns a jqXHR

setInterval(function () {
    $.getJSON('friendshipRequestCount', function(data) {
        var cnt=data.totalFriendshipRequestCount;
        if(cnt>0){
            $('#f-request-count').css("display","block");
            $('#f-request-count').html(data.totalFriendshipRequestCount);
        }
        else{
            $('#f-request-count').css("display","none");
        }
    });
}, 5000);

Upvotes: 2

user1726343
user1726343

Reputation:

setInterval needs to be passed a function, not the result of getJSON.

setInterval(function () {
    $.getJSON('friendshipRequestCount', function (data) {
        var cnt = data.totalFriendshipRequestCount;
        if (cnt > 0) {
            $('#f-request-count').css("display", "block");
            $('#f-request-count').html(data.totalFriendshipRequestCount);
        } else {
            $('#f-request-count').css("display", "none");
        }
    });
}, 5000);

Upvotes: 4

Related Questions