Jump to content

[SOLVED] Countdown timer function --- NaN


galvin

Recommended Posts

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.

Link to comment
Share on other sites

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();
}

Link to comment
Share on other sites

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);
	}
}


Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.