tags:

views:

99

answers:

4

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.

+3  A: 

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

Pekka
A: 

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)
extraneon
A: 

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

Ravia
A: 

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.... 
} 
naivists
Thankyou... that was indeed the issue :-)
Dooie