我想要實現的是最初的數據將被加載,然後每10分鐘使用相同的函數進行更新。在JavaScript中使用setInterval而不使用內聯,匿名函數
考慮以下代碼:
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);
我得到這個錯誤:
useless setInterval call (missing quotes around argument?)
我該如何解決這個問題?
什麼是更好更優雅的書寫方式或使用setInterval
函數?
謝謝。
不錯的答案!在我的情況下,我不得不將參數發送到由setInterval調用的函數。然後,我把它們放在更大的範圍內(不是全局思想)。它充當魅力! – 2012-12-10 13:50:57