- Home /
Count Down Timer javascript explaination?
i've been searching all over the Answer for countdown timer, but the most better code i could understand a little is this code.. but now i don't understand how to stop it.. so the code is here..
var seconds = 60;
private var textMesh : TextMesh;
function Start () {
textMesh = GameObject.Find ("Timer").GetComponent(TextMesh);
textMesh.text = seconds.ToString();
InvokeRepeating ("Countdown", 1.0, 1.0);
}
function Countdown () {
if (--seconds == 0) CancelInvoke ("Countdown");
textMesh.text = seconds.ToString();
//if(Input.GetKeyDown("a")){
// CancelInvoke("Countdown");
//}
}
So i want to stop the timer for the rest of the time, and store the value to any variable.. but i'm so confused how to do it.. could anybody help me to explain a bit which way to do this?
Answer by robertbu · Jan 14, 2014 at 08:33 PM
Add this to the above script. This will stop it when the 'S' key is pressed.
function Update() {
if (Input.GetKeyDown(KeyCode.S)) {
CancelInvoke("Countdown")
Debug.Log("Remaining time is: "+seconds);
}
}
Your answer
Follow this Question
Related Questions
Prevent countdown from reloading every time a new scene is loaded 1 Answer
How to stop a timer with GUI button? 1 Answer
How to stop a for loop before it completes it's full cycle? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
How to stop a function and start a countdown in a 2D game? 1 Answer