0

I've got text entry box and a countdown on the same page. I want to take the time from the box and enter it into the counter. I've got a variable back from the text box "setTime". I wanted to put that directly into my timeSplit function (to convert the time into seconds) but when I do I get an error that "time.split is not a function". What am I doing wrong here?

When I have a static variable enter the function (e.g. time = "12:12:12") everything works perfectly. - except its not using the right time

When I run the pop up alert on setTime before the timeSplit function I see my time like this "12:12:12" so its coming from the counter without a problem and I don't get a NaN error

Why would a time variable work when its static but not when its passed

I tried converting the setTime to a string but that just lead to NaN errors even when I tried to convert the sec variable back to an int.

I think this is the relevant code let me know if you need more.

var setTime = 0;
var $fromTime = $("#formEntry");

$("#setTime").off("click").on("click", function(){
  setTime = $fromTime.val();
});

function timeSplit(){
  //time = "12:12:12";
  tt = time.split(":");
  sec = tt[0]*3600+tt[1]*60+tt[2]*1;
  return sec;
}

var time = setTime;
//var time = "12:12:12";
var sec = timeSplit(time);

1 Answer 1

3

Your function timeSplit() does not take any arguments. It needs to be timeSplit(time) so that JavaScript knows you are talking about calling the method .split() on an object called time rather than a function just called time.split().

If this wasn't just a typo (I've done that before), I suggest you read up some on function arguments and parameters so you know you understand how this works, it's really important.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.