我试图传递变量我从chrome.storage.local.get到达另一个全局变量delayMilliSeconds
这样我就可以在多种功能中使用它。我知道delay
只是内部chrome.storage.local.get的范围和是异步的,但是,有没有办法通过这个范围之内?的Javascript传递一个变量chrome.storage.local.get到另一个变量
var delay;
chrome.storage.local.get('updateDelayValueTo', function(result){
delay = result.updateDelayValueTo; //I am getting this correctly
console.log("delay is: " + delay); //10000
});
function runMyCalculation() {
var delayMilliSeconds = Math.floor((Math.random() * delay) + 1);
// Run other code/functions, which depends on "delayMilliSeconds"
functionOne(delayMilliSeconds);
}
functionOne(delayMilliSeconds) {
//use delayMilliSeconds here
if(functionTwo()){
setTimeout(functionOne,delayMilliSeconds);
console.log("delay in here is: " + delayMilliSeconds);
}
}
functionTwo() {
//and here..
while(ButtonIsClicked){
console.log("delay in here is: " + delayMilliSeconds);
ButtonIsClicked logic...
}
}
console.log
delay down here is: 260
09:36:22.812 content_script.js:83 delay down here is: 260
09:36:22.813 content_script.js:15 delay is: 1000
09:36:23.074 content_script.js:79 delay down here is: undefined
09:36:23.087 content_script.js:83 delay down here is: undefined
09:36:23.089 content_script.js:79 delay down here is: undefined
09:36:23.089 content_script.js:83 delay down here is: undefined
如果我们声明'delay'外'chrome.storage.local.get'和内部分配的值,所以它服务的价值出方的范围。 – Amogh
或者干脆写一个全局函数从铬存储返回值,并用它每一个地方... – Amogh
@Amogh你能告诉我你的第二个方法的一个例子。 – kkmoslehpour