I have been working on a Unity project for sometime, I have a timer which currently counts down from around 30 minutes, however I want this to be 4 hours counting down. here is my code:
seconds = Mathf.CeilToInt (gameTimer - Time.timeSinceLevelLoad) % 60;
minutes = Mathf.CeilToInt (gameTimer - Time.timeSinceLevelLoad) / 60;
}
if (seconds == 0 && minutes == 0) {
chestfree.gameObject.SetActive(true);
checker = 2;
}
remainingTime = string.Format("{0:00} : {1:00}", minutes, seconds);
if (seconds != 0 && minutes != 0) {
h.text = remainingTime.ToString ();
} else {
h.text = "Get";
}
So you can see I have seconds and minutes in there, I tried to change the / 60 after minutes to 19 and when it ran it displayed 240 minutes which was fine, but the countdown seems to be very odd, minutes go down after around 20 seconds so I know thats not the right way to do it!
Can anyone explain to me how to add in hours or how I can make the minutes count down from 240 minutes?
Many thanks!
Untested code below:
hours = Mathf.FloorToInt((float)(gameTimer - Time.timeSinceLevelLoad) / 3600);
minutes = Mathf.FloorToInt((float)(gameTimer - Time.timeSinceLevelLoad-hours*3600) / 60);
seconds = Mathf.FloorToInt((gameTimer - Time.timeSinceLevelLoad) % 60);
if (seconds == 0 && minutes == 0 && hours == 0) {
chestfree.gameObject.SetActive(true);
checker = 2;
}
//remainingTime = string.Format("{0}:{1}:{2}",hours,minutes,seconds);
remainingTime = string.Format("{2:00} : {0:00} : {1:00}", minutes, seconds,hours);
if (seconds != 0 && minutes != 0 && hours != 0) {
h.text = remainingTime.ToString ();
} else {
h.text = "Get";
}
I'm not sure about the construction of the remainingTime string. I would have used something like this:
remainingTime = string.Format("{0}:{1}:{2}",hours,minutes,seconds);
EDIT:
(gameTimer - Time.timeSinceLevelLoad)/60 is an integer operation, that's why it did not work. By forcing float operation it should work.
Related
I want to do a program in C# - WFA, in which while a button is pressed - the label 1 value is increased by 10 since it reaches 100, then in decreases to 0 and again is increased til 100
like: 0 10 20 30...80 90 100 90 80...20 10 0 10...
i tried this:
private static int i = 0;
protected void button1_MouseClick(object sender, MouseEventArgs e)
{
if (i < 100)
i = i + 10;
else
i = i - 10;
this.label1.Text = i.ToString();
}
but it just decrease from 100 to 90 and the increases back to 100 and goes like this
You need to save two state information about the current value outside the button1_MouseClick() method. One is the value i, which you already do. The other is the information if you are going up or down. You can save this in a simple bool value like:
public static bool goingUp=true;
Then you can use these two fields i and goingUp and decide what you want to do:
if (goingUp)
{
i += 10;
if (i >= 100)
{
goingUp = false;
}
}
else
{
i -= 10;
if (i <= 0)
{
goingUp = true;
}
}
this.label1.Text=i.ToString();
an easy way would be this one
_indexValue = (_indexValue + 1) % 100;
and when it hits 100 then it restart to 0
When condition v is higher than 30 it keep sending SMS in app.
However, I want it to only send 1 SMS, then resend SMS when v goes back to 29, then 30.
if(v >= 30)
{
do
{
var SmsMessenger = (CrossMessaging.Current.SmsMessenger);
if(SmsMessenger.CanSendSmsInBackground)
{
SmsMessenger.SendSmsInBackground("+000000", "Test")
}
}
while(((currentLocation.Speed * 3600) / 1000) != 20);
}
You can use a flag, to call the message once.
bool IsSmsSended = false;
if(v >= 30 && !IsSmsSended) {
do {
var SmsMessenger= (CrossMessaging.Current.SmsMessenger);
if(SmsMessenger.CanSendSmsInBackground){
SmsMessenger.SendSmsInBackground("+000000","Test")
}
}
while(((currentLocation.Speed * 3600) /1000) !=20);
IsSmsSended = true;
}else if(v < 30){
IsSmsSended = false;
}
You have to reset IsSmsSended, if the value is under 30. Look at the else if.
P.S.: Make IsSmsSended global.
I am working on online Q/A system,i have to show countdown for each candidate like 3 min,on expiring user will be redirect to Result.aspx page.
I am facing following
1.how to set counter for each candidate.
2.on page refresh counter set to default value.
i have following code
<div id="timer">
</div>
<script type="text/javascript">
function countdown(minutes) {
var seconds = 60;
var mins = minutes;
if (getCookie("minutes") && getCookie("seconds")) {
var seconds = getCookie("seconds");
var mins = getCookie("minutes");
}
function tick() {
var counter = document.getElementById("timer");
setCookie("minutes", mins, 10)
setCookie("seconds", seconds, 10)
var current_minutes = mins - 1
seconds--;
counter.innerHTML =
current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
//save the time in cookie
//minutesSpan.innerHTML = current_minutes.toString();
//secondsSpan.innerHTML = (seconds < 10 ? "0" : "") + String(seconds);
if (seconds > 0) {
setTimeout(tick, 1000);
}
else {
if (mins > 1) {
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithorst
setTimeout(function () { countdown(mins - 1); }, 1000);
}
}
}
tick();
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
countdown(3);
</script>
because multiple user will doing these test so i have handle each one differently,i have following code to assign to assign test for each candidate
private void NewCandidate()
{
var ClistCount = (from cl in ef.Candidate_Table where cl.Name == btnname.Text && cl.Status_Id==2 select cl).FirstOrDefault();
if (ClistCount != null)
{
string cnic = ClistCount.Cnic;
Session["token"] = cnic;
Response.Redirect("MainTestForm.aspx?id=" + cnic);
}
else
{
MsgLitteral.ShowNotification("No Candidate Is Here", true);
btnbrowse.Visible = false;
btnname.Visible = false;
}
}
There are two things which you need to do in order to make things working
1) Create perfectly working count down timer method
2) Solve the reload dependency
for reload thing just before reload/refresh, trigger a function that would store the current time elapsed from the predefined count down.
$(window).bind('beforeunload', function(){
//below function stores current elapsed time in cookie/local storage
callFunction();
return true;
});
for e.g if 3min countdown is set and the user refreshes or moves to
next question at 2min 40 secs then store 2min 40 sec in cookies or
html5 local storage
On every document ready event check the cookie value
if value present then
take this value and set countdown
else
with predefined value (for the first time case)
A simple countdown timer for reference
In the top of form1 i have:
private int seconds;
private int minutes;
private int hours;
In the constructor:
seconds = 0;
minutes = 20;
hours = 0;
label9.Visible = false;
label9.Text = "00:00:00";
Then the timer 3 tick event. I used a breakpoint on the timer3 tick event i see that the minutes and seconds are coundting backwards but label 9 is not updating.
Maybe the string.Format on label9 is not right ?
private void timer3_Tick(object sender, EventArgs e)
{
// Verify if the time didn't pass.
if ((minutes == 0) && (hours == 0) && (seconds == 0))
{
// If the time is over, clear all settings and fields.
// Also, show the message, notifying that the time is over.
timer3.Enabled = false;
label9.Visible = true;
label9.Text = "00:00:00";
}
else
{
// Else continue counting.
if (seconds < 1)
{
seconds = 59;
if (minutes == 0)
{
minutes = 59;
if (hours != 0)
hours -= 1;
}
else
{
minutes -= 1;
}
}
else
seconds -= 1;
// Display the current values of hours, minutes and seconds in
// the corresponding fields.
label7.Visible = true;
label9.Visible = true;
label9.Text = string.Format("{00} : {00} : {00}", hours.ToString(), minutes.ToString(),seconds.ToString());
}
}
All i see in the end on label 9 is: 0:0:0 thats it and i dont see 20 minutes counting backward.
You need to fix your string formatting, that's the culprit:
label9.Text = string.Format("{0:00} : {1:00} : {2:00}", hours, minutes, seconds);
Also - consider using some other mechanism (e.g. Stopwatch class) for calculating elapsed time; you can't guarantee that the code as written will execute exactly every 1000 ms.
I Have This in C#
private void counter_Tick(object sender, EventArgs e)
{
Time.Text = String.Format("{0:000}", Hour) + ":" + String.Format("{0:00}", Minute) + ":" + String.Format("{0:00}", Second);
if (Second != 00)
{
Second = Second - 1;
}
else if (Minute != 00)
{
Minute = Minute - 1;
Second = 59;
}
else if (Hour != 00)
{
Hour = Hour - 1;
Minute = 59;
}
else
{
counter.Stop();
Time.ForeColor = Color.Red;
}
}
Which Does work but when it gets to minus an hour to add to minutes, it goes from 00 minutes to 58 minutes instead of 59
EG.
From: 001:00:00
To: 000:58:59
And is there a better way to make a countdown timer that does something when it hits 000:00:00???
Well let's see what happens when the time is 10:00:00.
Subtract one hour: 09:00:00.
Set minutes to 59: 09:59:00.
If you notice the time is off by one minute (10:00:00 - 09:59:00 = 00:01:00). The solution is to set the seconds to 59 also. So now our code is.
// ...
else if (Hour != 00)
{
Hour = Hour - 1;
Minute = 59;
Second = 59;
}
// ...
You can use standard .Net classes for subtracting time:
private TimeSpan timeSpan;
private TimeSpan oneSecond = new TimeSpan(0, 0, 1);
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Time.Text = timeSpan.ToString();
if (timeSpan == TimeSpan.Zero)
{
Time.ForeColor = Color.Red;
timer.Stop();
return;
}
timeSpan -= oneSecond;
}
Initialize timespan when you starting your timer (I used System.Timers.Timer):
timeSpan = new TimeSpan(1, 0, 0);
Timer timer = new Timer(1000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
You also need to set Second to 59. Else, once the timer ticks again, it immediately switches to else if (Minute != 00) and decrements Minute (which is already 59) by one.
DateTime start;
DateTime final;
private void start()
{
start = DateTime.Now;
final = start + TimeSpan.FromHours(1);
}
private void counter_Tick(object sender, EventArgs e)
{
start = DateTime.Now;
Time.Text = (final-start).Hours.ToString() + ":" + (final-start).Minutes.ToString() + ":" + (final-start).Seconds.ToString();
if (final == start)
{
//final code
}
}