" typeof pageViewCount"显示"数字"而" pageViewCount"显示" NaN"但我不知道为什么。 代码如下所示。
var pageViewCount = localStorage.getItem("pageViewCount");
var pageFirstVisited = localStorage.getItem("pageFirstVisited");
var now = new Date();
if(pageViewCount == null){
pageViewCount = 0;
pageFirstVisited = now.toUTCString();
}
var oneMonth = new Date(pageFirstVisited);
oneMonth.setMonth(oneMonth.getMonth()+1);
if(now>oneMonth){
pageViewCount = 0;
pageFirstVisited = now.toUTCString();
}
pageViewCount = parseInt("pageViewCount",10)+1;
alert(pageViewCount);
localStorage.setItem("pageViewCount",pageViewCount);
localStorage.setItem("pageFirstVisited",pageFirstVisited);
var output = "You've visited this page " + pageViewCount + " times since" + pageFirstVisited;
document.write(output);
答案 0 :(得分:1)
你正在使用parseInt()
pageViewCount = parseInt("pageViewCount",10)+1;
您应该将其更改为
pageViewCount = parseInt(pageViewCount,10) + 1;
答案 1 :(得分:-1)
基本上,NaN是一个数值数据类型值,它告诉数字值不可计算或可显示。 https://en.wikipedia.org/wiki/NaN