JavaScript 是 NaN,但我知道它不是?
我有以下两个函数...
function splitTitleString(titleText)
{
var titleText = titleText;
var temp = new Array();
temp = titleText.split(' - ');
var now = new Date().getTime();
var warningExpResp = 7200000;
var expRespDateTimeTicks = 0;
var slaDateTimeTicks = 0;
if(temp[0].length > 0)
{
slaDateTimeTicks = getTicks(temp[0]);
}
if(temp[1].length > 0)
{
expRespDateTimeTicks = getTicks(temp[1]);
}
var returnTicksArray = new Array(slaDateTimeTicks,expRespDateTimeTicks);
return returnTicksArray;
}
而且...
function warning(titleText, serverDateTime, warningLengthMins, warningType)
{
var warningLengthTicks = warningLengthMins * (60 * 1000);
var ticks = new Array(splitTitleString(titleText));
var sla = parseInt(ticks[0]);
var resp = parseInt(ticks[1]);
var serverTicks = getTicks(serverDateTime);
// some other work....
}
我遇到的问题是 'resp' 始终为 NaN,即使 'ticks1' 最肯定的是?
PS:我不是 JavaScript 开发人员,所以如果代码很糟糕,请保持友善。
I have the following two functions...
function splitTitleString(titleText)
{
var titleText = titleText;
var temp = new Array();
temp = titleText.split(' - ');
var now = new Date().getTime();
var warningExpResp = 7200000;
var expRespDateTimeTicks = 0;
var slaDateTimeTicks = 0;
if(temp[0].length > 0)
{
slaDateTimeTicks = getTicks(temp[0]);
}
if(temp[1].length > 0)
{
expRespDateTimeTicks = getTicks(temp[1]);
}
var returnTicksArray = new Array(slaDateTimeTicks,expRespDateTimeTicks);
return returnTicksArray;
}
And...
function warning(titleText, serverDateTime, warningLengthMins, warningType)
{
var warningLengthTicks = warningLengthMins * (60 * 1000);
var ticks = new Array(splitTitleString(titleText));
var sla = parseInt(ticks[0]);
var resp = parseInt(ticks[1]);
var serverTicks = getTicks(serverDateTime);
// some other work....
}
The problem I have is 'resp' is always NaN even though 'ticks1' most definitely is?
PS: I'm not a JavaScript developer so please be nice if it's poor code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您在
warning()
函数之外访问resp
,则问题在于,通过使用var resp
,您定义了一个可见的新变量仅在包含函数内。如果 resp 应该是一个全局变量,删除var
关键字就可以了。请参阅此处,了解 Javascript 中的局部变量和全局变量的详细概述:变量范围和 var 关键字
If you are accessing
resp
outside thewarning()
function, your problem is that by usingvar resp
, you define a new variable that is visible only within the containing function. If resp is supposed to be a global variable, remove thevar
keyword and you should be fine.See here for a nice rundown on local and global variables in Javascript: Variable scope and the var keyword
您的
splitTitleString()
返回一个数组值。但是,当您定义
ticks
时,您会说varticks = new Array(splitTitleString(titleText));
。您定义一个由一个元素组成的数组 - splitTitleString 的返回值。结果,
ticks[0]
显然不是一个整数,它是一个数组!编辑:要修复它,请像这样重写您的函数:
Your
splitTitleString()
returns an array value.However, when you define
ticks
, you sayvar ticks = new Array(splitTitleString(titleText));
. You define an array which consists of one element- the return value of splitTitleString.As a result,
ticks[0]
is obviously not an integer, it is an array!EDIT: To fix it, rewrite your function like this:
除了 Pekka 正确所说的之外,parseInt 更喜欢有一个基数参数。来自文档:
Apart from what Pekka correctly said, parseInt prefers to have a radix parameter. From the docs:
您作为“titleText”参数传递什么,检查它是否确实是一个 int。我的意思是[1]
What are you passing as "titleText" parameter, check if it is indeed an int. I mean [1]