0

I have a method in javascript that use the split method to store the time in an array and then convert the time to seconds. But when I debug, the array always has first 2 elements and ignore the last one. Not sure why?

GetSeconds : function (time) { 
 var timesecs = 0;
 var min = 1; 
 var timeArray = time.split(ctx.options.separator); //this always contain 2 elements
 while (timeArray.length > 0) {
 timesecs += min * parseInt(timeArray.pop());
 min *= 60;
 }
 return timesecs; 
}

ctx.options.separator is a variable that stores my delimiter. I was trying with ":" and time passed was "00:00:00". This method is called from another method which increments the second.

I tried it in IE, Chrome and Firebug. This behaves differently when I debug through Visual Studio (as this code is in my .net app)

asked Mar 12, 2012 at 18:41

2 Answers 2

1

I tried a fiddle and everything works fine there. Are you sure, that ctx.options.separator works as expected?

answered Mar 12, 2012 at 18:48
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! Actually the problem was with parseInt(timeArray.pop()). parseInt was not able to parse the values greater than 8. We need to use base of 10 as parseInt(timeArray.pop(), 10). Also, I guess I had timeArray.pop() in watch.
What kind of weird-o parseInt defaults to base 8?
0

The problem may be a browser issue if ctx.options.separator is being generated properly. Which browser are you using?

Use this cross browser method that will make everything work as expected no matter which browser you use. http://blog.stevenlevithan.com/archives/cross-browser-split

It always has worked for me.

answered Sep 10, 2014 at 10:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.