galvin Posted April 27, 2009 Share Posted April 27, 2009 if I have a function... function cd(time) { mins = 1 * m("time"); // change minutes here secs = 0 + s(":01"); // change seconds here redo(); } and then I run... cd(5); it shows my countdown timer as NaN:59. I know that means not a number, but what am I doing wrong in the original function. Shouldn't I be able to pass it a number like I did (i.e. the 5)??? I didn't include the rest of the countdown timer code b/c I don't think it matters, but if you disagree, let me know and I'll attach all the code. Quote Link to comment https://forums.phpfreaks.com/topic/155842-solved-countdown-timer-function-nan/ Share on other sites More sharing options...
Psycho Posted April 27, 2009 Share Posted April 27, 2009 The line in which you use "time" you are using it as the string "time" and not the variable time. I'm assuming m() and s() are functions. function cd(time) { // You are passing the string "time" and not the var time mins = 1 * m("time"); // Passing a string here as well, but it includes numbers, //so it may be interpreted correctly secs = 0 + s(":01"); redo(); } Quote Link to comment https://forums.phpfreaks.com/topic/155842-solved-countdown-timer-function-nan/#findComment-820282 Share on other sites More sharing options...
galvin Posted April 27, 2009 Author Share Posted April 27, 2009 Here is the entire external file called "countDown.js". I guess my question is how could I call "cd()" from another page and be able to enter cd(any number I want) and then have it run the cd() function below and replace the "5" (on line 4 below) with whatever number I enter into cd(any number I want)? var mins var secs; function cd() { mins = 1 * m("5"); // change minutes here secs = 0 + s(":01"); // change seconds here redo(); } function m(obj) { for(var i = 0; i < obj.length; i++) { if(obj.substring(i, i + 1) == ":") break; } return(obj.substring(0, i)); } function s(obj) { for(var i = 0; i < obj.length; i++) { if(obj.substring(i, i + 1) == ":") break; } return(obj.substring(i + 1, obj.length)); } function dis(mins,secs) { var disp; if(mins <= 9) { disp = " 0"; } else { disp = " "; } disp += mins + ":"; if(secs <= 9) { disp += "0" + secs; } else { disp += secs; } return(disp); } function redo() { secs--; if(secs == -1) { secs = 59; mins--; } document.cd.disp.value = dis(mins,secs); // setup additional displays here. if((mins == 0) && (secs == 0)) { window.alert("Time is up. Press OK to continue."); // change timeout message as required // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed } else { cd = setTimeout("redo()",1000); } } Quote Link to comment https://forums.phpfreaks.com/topic/155842-solved-countdown-timer-function-nan/#findComment-820291 Share on other sites More sharing options...
Psycho Posted April 27, 2009 Share Posted April 27, 2009 I already gave you the solution. You are not properly utilizing the variable passed to the function. If you put a varibale name within quotes, it is no longer a variable, it is a string. JavaScript does not interpret variables within a string like PHP does. Here is an example of what I mean function alertVariableNoQuotes(var) { alert(var); //This will alert the value of the variable passed to the function } function alertVariableWithQuotes(var) { alert("var"); //This will alert the string "var" } This line in your code mins = 1 * m("time"); // change minutes here interprets "time" as a sting, NOT the value of the variable time. You would need to remove the quotes, however there are other parts of your code that requires the variable to be a string. I decided just to rewite the whole thing var seconds = 0; var outputObj = false; function startTimer(startTime, timerOutputObj) { //Parse minutes and seconds into integers startTime = String(startTime); var mins = parseInt(startTime.split(':')[0]); if (isNaN(mins)) { mins = 0; } var secs = parseInt(startTime.split(':')[1]); if (isNaN(secs)) { secs = 0; } //Convert to seconds seconds = (mins * 60) + secs; outputObj = timerOutputObj; //Run the timer runTimer(outputObj); } function runTimer() { outputObj.value = formatTime(seconds); seconds--; if(seconds < 0) { window.alert("Time is up. Press OK to continue."); // change timeout message as required // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed } else { setTimeout('runTimer()', 1000); } } function formatTime(seconds) { var mins = Math.floor(seconds/60); if (mins<10) { mins = '0' + mins; } var secs = Math.round(seconds%60); if (secs<10) { secs = '0' + secs; } return (mins + ':' + secs); } You would call the function like this startTimer('5:30', document.cd.disp); The start time can be a string or a number. Quote Link to comment https://forums.phpfreaks.com/topic/155842-solved-countdown-timer-function-nan/#findComment-820402 Share on other sites More sharing options...
galvin Posted April 28, 2009 Author Share Posted April 28, 2009 All I can say is THANK YOU, THANK YOU, THANK YOU!!! Works like a charm and I understand it better now as well. I don't know if you get paid to help newbies like me, but you should. This site is amazing. Quote Link to comment https://forums.phpfreaks.com/topic/155842-solved-countdown-timer-function-nan/#findComment-821440 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.