2012-08-16 127 views
0

我想调用一个函数两次,但与当前的代码我getPoints()函数被跳过,setInterval中的getPoints()函数被调用。我试图让它调用getPoints()函数,然后调用setInterval中的一个。调用一个函数两次

var background; 
// background page 
background = chrome.extension.getBackgroundPage(); 
// get totals 
getPoints(background.localStorage.points); 
// update every 30 seconds 
setInterval(function() { 
    console.log("func"); 
    getPoints(background.localStorage.points); 
}, 30000); 
+0

你检查错误的JavaScript控制台?我的猜测是getPoints函数是在代码运行后定义的。这会导致第一次调用失败,但是setInterval调用会成功,因为函数是在间隔被命中时定义的。 – tsholmes 2012-08-16 05:18:30

+0

您可以在函数getPoints()中给出一些'console.log'来确定它是否正在调用或由于其他原因而终止 – 2012-08-16 05:18:38

回答

1

试试这个:

window.onload = function() { 
    var background; 
    // background page 
    background = chrome.extension.getBackgroundPage(); 
    // get totals 
    getPoints(background.localStorage.points); 
    // update every 30 seconds 
    setInterval(function() { 
     console.log("func"); 
     getPoints(background.localStorage.points); 
    }, 30000); 
}; 
+0

This works :)谢谢 – 2012-08-16 05:23:59