我正在尝试进行倒计时扩展,在background.js中运行countdwn,这样当用户点击扩展时,计时器仍在继续。我可以在一个点上获得正确的倒计时时间,但我需要能够继续获得倒计时时间,以便扩展程序不断更新弹出窗口中的时间。我已经尝试了下面的while循环,但由于我是js和chrome扩展的初学者,我无法弄清楚为什么这不起作用。
popup.js
function countdown_init(e) {
update_counter();
}
function update_counter(){
while(is_timer_running!=false){
chrome.extension.sendRequest({method: "start_timer"}, function(response) {
countdown_number = response;
is_timer_running = true;
});
days = Math.floor(countdown_number/(3600*24));
hours = (Math.floor(countdown_number/(3600))-days*24) % 24;
minutes = (Math.floor(countdown_number/(60))-hours*60) % 60;
seconds = (Math.floor(countdown_number)-minutes*60) % 60;
document.getElementById('time_numbers').innerHTML = days_ + " | " + hours_ + " | " + minutes_ + " | " + seconds_;
}
}
background.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "LocalStorage"){
time = get_Item(1);
sendResponse(item);
}
if(request.method="start_timer"){
countdown_start();
sendResponse(countdown_number);
}
if(request.method="stop_timer"){
countdown_stop();
sendResponse(countdown_number);
}
});