如何解决Undefined和Nan? [英] How to resolve Undefined and Nan?

查看:489
本文介绍了如何解决Undefined和Nan?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Chrome扩展程序,并在弹出窗口中遇到以下错误。



I清楚地定义了'total'变量,并在其中存储来自本地存储的值。但是,为什么我
得到这样的错误?

m试图做的:

<1>我将总变量存储在本地存储中,并从popup.js访问它。
2)然后修改popup.html中的< p> 元素



popup.html和popup.js:


$ b popup.js:

  var total,percentage; 

chrome.storage.local.get('myVariable',function(items){
total =(items.myVariable);
});



百分比=总数/ 7.25;

document.getElementById(total)。innerHTML = total;
document.getElementById(percentage)。innerHTML = percentage;

popup.html:

 <!DOCTYPE HTML> 

< html>
< head>
< title>弹出式页面< / title>
< / head>
< body>

< form name =F1method =POST>
TOTAL:< p id =total>< / p>
PERCENTAGE:< p id =percentage>< / p>
< / form>

< script src =popup.js>

< / script>

< / body>
< / html>

我的contentscript.js中的片段

  chrome.storage.local.set({
'myVariable':total;
});
chrome.runtime.sendMessage({
greeting:myAction
});

在background.js中,我有这个:

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request.greeting ==myAction){
collectData();
}
});

在访问本地存储之前,我们应该在popup.js中添加一个侦听器。我添加并检查,但
我得到相同的结果。
这段代码有什么问题?
如何完成我要做的事情?



我的扩展程序的本地存储:



chrome.storage.local.get 回调是异步执行的,因此在尝试用它。如果将脚本的其余部分移到回调函数中,它应该可以正常工作。

  var total,percentage; 

chrome.storage.local.get('myVariable',函数(items){
total =(items.myVariable);

百分比=总数/ 7.25 ;

document.getElementById(total)。innerHTML = total;
document.getElementById(percentage)。innerHTML = percentage;
});


I'm building an Chrome extension and I encountered following error in my popup.

I clearly defined 'total' variable and storing in it a value from local storage.But why am I getting such an error?

What I'm trying to do:

1)I'm storing total variable in local storage and access it from popup.js. 2)Then modify <p> elements in popup.html

Here is popup.html and popup.js:

popup.js:

var total,percentage;

chrome.storage.local.get('myVariable', function (items) { 
total = (items.myVariable);
});



percentage = total/7.25;

document.getElementById("total").innerHTML = total;
document.getElementById("percentage").innerHTML = percentage;

popup.html:

<!DOCTYPE HTML>

<html>
  <head>
  <title>Popup page</title>
  </head>
  <body>

  <form name="F1" method="POST">
        TOTAL  : <p id="total"></p>
    PERCENTAGE : <p id="percentage"></p>    
  </form>

  <script src="popup.js">

  </script>

  </body>
</html>

Snippet from my contentscript.js

 chrome.storage.local.set({
         'myVariable': total;
        });
 chrome.runtime.sendMessage({
     greeting: "myAction"
 });

In background.js I've got this :

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
    if (request.greeting == "myAction") {
        collectData();
    }
 });

Should we add a listener to popup.js before accessing local storage. I added and checked but I'm getting the same result. What's wrong with this code? How can accomplish what I'm set to do?

Local storage of my extension:

解决方案

The chrome.storage.local.get callback is executed asynchronously so total doesn't get defined until after you try to use it. If instead move the rest of the script into the callback it should work.

var total,percentage;

chrome.storage.local.get('myVariable', function (items) { 
    total = (items.myVariable);

    percentage = total/7.25;

    document.getElementById("total").innerHTML = total;
    document.getElementById("percentage").innerHTML = percentage;
});

这篇关于如何解决Undefined和Nan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆