JavaScript 是 NaN,但我知道它不是?

发布于 2024-08-17 11:06:24 字数 1247 浏览 7 评论 0原文

我有以下两个函数...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白昼 2024-08-24 11:06:24

如果您在 warning() 函数之外访问 resp,则问题在于,通过使用 var resp,您定义了一个可见的新变量仅在包含函数内。如果 resp 应该是一个全局变量,删除 var 关键字就可以了。

请参阅此处,了解 Javascript 中的局部变量和全局变量的详细概述:变量范围和 var 关键字

If you are accessing resp outside the warning() function, your problem is that by using var resp, you define a new variable that is visible only within the containing function. If resp is supposed to be a global variable, remove the var 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

猫弦 2024-08-24 11:06:24

您的 splitTitleString() 返回一个数组值。

但是,当您定义 ticks 时,您会说 varticks = new Array(splitTitleString(titleText));。您定义一个由一个元素组成的数组 - splitTitleString 的返回值。

结果,ticks[0]显然不是一个整数,它是一个数组!

编辑:要修复它,请像这样重写您的函数:

function  warning(titleText, serverDateTime, warningLengthMins, warningType) 
{ 
    var warningLengthTicks = warningLengthMins * (60 * 1000); 
    //I removed the "new Array()" from the next line
    var ticks = splitTitleString(titleText); 
    var sla = parseInt(ticks[0]); 
    var resp = parseInt(ticks[1]); 
    var serverTicks = getTicks(serverDateTime); 

    // some other work.... 
} 

Your splitTitleString() returns an array value.

However, when you define ticks, you say var 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:

function  warning(titleText, serverDateTime, warningLengthMins, warningType) 
{ 
    var warningLengthTicks = warningLengthMins * (60 * 1000); 
    //I removed the "new Array()" from the next line
    var ticks = splitTitleString(titleText); 
    var sla = parseInt(ticks[0]); 
    var resp = parseInt(ticks[1]); 
    var serverTicks = getTicks(serverDateTime); 

    // some other work.... 
} 
沉默的熊 2024-08-24 11:06:24

除了 Pekka 正确所说的之外,parseInt 更喜欢有一个基数参数。来自文档:

如果省略基数参数,
JavaScript 假设如下:

  • 如果字符串以“0x”开头,则
    基数为 16(十六进制)
  • 如果
    字符串以“0”开头,基数为8
    (八进制)。此功能已弃用
  • 如果字符串以任何其他字符串开头
    值,基数为10(十进制)

Apart from what Pekka correctly said, parseInt prefers to have a radix parameter. From the docs:

If the radix parameter is omitted,
JavaScript assumes the following:

  • If the string begins with "0x", the
    radix is 16 (hexadecimal)
  • If the
    string begins with "0", the radix is 8
    (octal). This feature is deprecated
  • If the string begins with any other
    value, the radix is 10 (decimal)
爱的故事 2024-08-24 11:06:24

您作为“titleText”参数传递什么,检查它是否确实是一个 int。我的意思是[1]

What are you passing as "titleText" parameter, check if it is indeed an int. I mean [1]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文