Jump to content

[SOLVED] Stop a function?


galvin

Recommended Posts

I have this external JS file for a Countdown Timer which works fine....

 

// JavaScript Document

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's up! Press OK to reveal missed answers."); // change timeout message as required
        // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed

	reveal();//does this go back to the 	

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

 

 

This main function in this external file get called by ...

 

function beginthegame(time) {
startTimer(time, document.cd.disp);
}

 

The Timer starts fine, but there are certain instances where I need the Timer to stop.  So, how do I STOP an existing running function?  In layman's terms, how would I stop this Timer before it hit 00:00.

 

I ask because certain code runs when the timer hits 00:00, and like I said, sometimes I don't want that code to run.

 

I can hide the timer display, but I found that it still runs behind the scenes, so I need a way to completely STOP it.

 

Any ideas?

Link to comment
Share on other sites

No, for instance, people are entering answers on my site and they have a certain amount of time to do it.  If time "runs out", certain code runs.  But if someone enters all of the right answers BEFORE the time runs out, I run other code.  In this "other" code, I'd like to add something that causes the timer function to stop running behind the scenes, otherwise unnecessary code will run.

 

So the timer could be at anytime (00:10, or 00:44, etc, etc, etc) when I need it to stop running.

 

I'm sure this isn't enough information and I apologize for that. 

Link to comment
Share on other sites

Change your code to:

// JavaScript Document

var seconds = 0;
var outputObj = false;
var timeoutObj;

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's up! Press OK to reveal missed answers."); // change timeout message as required
        // window.location = "yourpage.htm" // redirects to specified page once timer ends and ok button is pressed

      reveal();//does this go back to the    
   
    }
    else
    {
        timeoutObj = 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);
}

 

Then when you want to stop,

if (timeoutObj) clearTimeout(timeoutObj);

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.