What I'm trying to achieve is that initially data will be loaded and then be updated every ten minutes using the same function.
Consider this code:
var updateNamespace = (function() {
var object = '#updates',
load = 'loader';
return {
update: function() {
$(object).addClass(load).load('update.php', function(reponse, status, xhr) {
if (status == 'error') {
$(this).html('<li>Sorry but there was an error in loading the news & updates.</li>');
}
$(this).removeClass(load);
});
}
}
})();
setInterval(updateNamespace.update(), 600000);
I get this error:
useless setInterval call (missing quotes around argument?)
How can I fix this?
What's a better and more elegant way of writing this or using the setInterval
function?
Thanks.
You need to use:
setInterval(updateNamespace.update, 600000);
(Note the removed invocation() operator.)
Your code, as written, will actually invoke updateNamespace.update
when you call setInterval. Hence,
setInterval(updateNamespace.update(), 600000);
evaluates to
setInterval(undefined, 600000);
You want to pass setInterval
a REFERENCE to your function, not the result of its invocation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With