-->

JQuery & Timer :: Updating the text of a hyperlink

2019-07-13 16:28发布

问题:

What I'm to figure out how to do is execute a webservice 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://foobar.com/ws',success:function(messageCount) 
    { 
        $('a.message').text(messageCount + ' Messages');
    }
})

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

Thanks!

回答1:

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');
        }
    })
}


回答2:

If you want to do this periodically you should use setInterval.

//create the timer (and save the unique id returned
//so we can stop it later if needed, using clearTimer())
//This will fire every 300,000 millisecs (= 5 mins) 
//and call the updateMessageCount function
var msgCounTimer = setInterval(updateMessageCount, 300000);

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

If however, you want to do it once use setTimeout - you could do it periodically with setTimeout too except that you'd need to make a call to set up the timer each time.

The one case I can think of where you might want to use setTimeout instead of setInterval would be when the work you're doing in the function called on timeout (updateMessageCount in my example below), takes longer than the timeout interval which would cause the next timeout (when using setInterval) to appear to be instantaneous since the timer is going to fire every n seconds irrespective of whether the function called by the previous timeout is still executing. If instead, you wanted to ensure that there will be n milliseconds between consecutive calls to your timeout handler (as opposed to your timeout handler getting called every n milliseconds), you should be using setTimeout instead and setting it up every time you finish your heavy processing.

Note I see you're using an absolute URL in the example so I thought I'd point this out in case you're not familiar with it - watch out for the same-origin policy which restricts your JavaScript from making requests to domains other than the one from which it originates.



回答3:

You need to look at the setTimeout function in Javascript

e.g var t=setTimeout("yourfunction()",10000);