English 中文(简体)
JavaScript is a NaN, but I know it s not?
原标题:

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.

最佳回答

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.... 
} 
问题回答

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

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)

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





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签