Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery & Timer :: Updating the text of a hyperlink from a webservice

What I'm to figure out how to do is execute a web service on a remote server to determine how many messages there are available to read and populate the text of a hyperlink with the message count. The trick is I'd like this to be fired from a timer (every 5 mins for example). Here's what I've been messing with

$.ajax( {type:'Get',url:'http://example.com/ws',success:function(messageCount) 
    { 
        $('a.message').text(messageCount + ' Messages');
    }
})

but admittedly, I am utterly clueless when it comes to timers in Javascript/jQuery. Any help would be greatly appreciated.

like image 296
Andy Evans Avatar asked Nov 21 '25 22:11

Andy Evans


1 Answers

You need to use the setInterval() function. This will run a defined function at the specified interval. Try this:

$(function() {
    setInterval("getDataFromWS()", 300000) // 300,000 miliseconds is 5 minutes
});

function getDataFromWS() {
    $.ajax({
        type:'Get',
        url:'http://foobar.com/ws',
        success:function(messageCount) { 
            $('a.message').text(messageCount + ' Messages');
        }
    })
}
like image 147
Rory McCrossan Avatar answered Nov 23 '25 16:11

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!