Countdown

All Party-approved topics were welcome
Post Reply
User avatar
fer
Host
Posts: 1709
Joined: Fri Jun 04, 2010 8:16 am
Location: Emotional wreck

Countdown

Post by fer » Sat Aug 28, 2010 11:52 am

Countdown? What? What's new?
If you look at the header of this forum, you might notice something different. No, not the part that says "Lor is Awesome. Tribute to Lor." That's new too, but that's not the bit I'm talking about. Check out the countdown to the left of that. Neat, eh? Also, I put this up on what would be the folk homepage, if folk had a homepage: http://www.ferstaberinde.com/folk/

Is there a man in a bunker somewhere, who has to press a button each time the count reaches zero?
Where do you think Lor's been all this time?

User avatar
fer
Host
Posts: 1709
Joined: Fri Jun 04, 2010 8:16 am
Location: Emotional wreck

Re: Countdown

Post by fer » Mon Aug 30, 2010 11:03 am

The counter is telling me I'm too late. Is it broken, or has folk ended for good?
Don't panic, folk is still here! The counter's configuration options won't let me set it up to count down to the same time every Sunday. I'm going to post the code below, in the hope that one of our guests can suggest an edit that will save me from having to manually change the script each week:

Code: Select all

var month = '*'; // 1 through 12 or '*' within the next month, '0' for the current month
var day = '0';   // day of month or + day offset
var dow = 1;     // day of week sun=1 sat=7 or 0 for whatever day it falls on
var hour = 20;    // 0 through 23 for the hour of the day
var min = 30;    // 0 through 59 for minutes after the hour
var tz = 1;     // offset in hours from UTC to your timezone
var lab = 'cd';  // id of the entry on the page where the counter is to be inserted

function start() {displayCountdown(setCountdown(month,day,hour,min,tz),lab);}
loaded(lab,start);

// Countdown Javascript
// copyright 20th April 2005, 1st November 2009 by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
// you may change the start function if required
var pageLoaded = 0; window.onload = function() {pageLoaded = 1;}
function loaded(i,f) {if (document.getElementById && document.getElementById(i) != null) f(); else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}
function setCountdown(month,day,hour,min,tz) {var m = month; if (month=='*') m = 0;  var c = setC(m,day,hour,tz); if (month == '*' && c < 0)  c = setC('*',day,hour,tz); return c;} function setC(month,day,hour,tz) {var toDate = new Date();if (day.substr(0,1) == '+') {var day1 = parseInt(day.substr(1));toDate.setDate(toDate.getDate()+day1);} else{toDate.setDate(day);}if (month == '*')toDate.setMonth(toDate.getMonth() + 1);else if (month > 0) { if (month <= toDate.getMonth())toDate.setFullYear(toDate.getFullYear() + 1);toDate.setMonth(month-1);}
if (dow >0) toDate.setDate(toDate.getDate()+(dow-1-toDate.getDay())%7);
toDate.setHours(hour);toDate.setMinutes(min-(tz*60));toDate.setSeconds(0);var fromDate = new Date();fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());var diffDate = new Date(0);diffDate.setMilliseconds(toDate - fromDate);return Math.floor(diffDate.valueOf()/1000);}
function displayCountdown(countdn,cd) {if (countdn < 0) document.getElementById(cd).innerHTML = "Sorry, you are too late."; else {var secs = countdn % 60; if (secs < 10) secs = '0'+secs;var countdn1 = (countdn - secs) / 60;var mins = countdn1 % 60; if (mins < 10) mins = '0'+mins;countdn1 = (countdn1 - mins) / 60;var hours = countdn1 % 24;var days = (countdn1 - hours) / 24;document.getElementById(cd).innerHTML = days+'d '+hours+'h '+mins+'m '+secs;setTimeout('displayCountdown('+(countdn-1)+',\''+cd+'\');',999);}}
Does anyone have JavaScript skillz? :science101:

User avatar
fer
Host
Posts: 1709
Joined: Fri Jun 04, 2010 8:16 am
Location: Emotional wreck

Re: Countdown

Post by fer » Wed Sep 01, 2010 11:23 am

Also, the countdown is borked right now. Ignore it!

User avatar
Xiathorn
Posts: 67
Joined: Fri Jun 04, 2010 3:04 pm

Re: Countdown

Post by Xiathorn » Wed Sep 01, 2010 5:08 pm

I'm going to take a look at this. I've just spent the past 10 minutes unfucking the lack of linebreaks, so I'm about ready to commit genocide. I'll post up a proper working version when I can.

User avatar
Xiathorn
Posts: 67
Joined: Fri Jun 04, 2010 3:04 pm

Re: Countdown

Post by Xiathorn » Wed Sep 01, 2010 5:29 pm

This is the hackery I have so far. It doesn't actually work, but Girlthorn wants me to go eat. This will work, but overall what it needs is either a wrapper to control progression from week to week or just a total rewrite (that will also eliminate the excess that we don't need.)

I'll try to get this finished later today, but chances are I won't be able to (especially as I currently lack a test enviroment! :P)

Code: Select all

    var month = '*'; // 1 through 12 or '*' within the next month, '0' for the current month
    var day = '0';   // day of month or + day offset
    var dow = 1;     // day of week sun=1 sat=7 or 0 for whatever day it falls on
    var hour = 20;    // 0 through 23 for the hour of the day
    var min = 30;    // 0 through 59 for minutes after the hour
    var tz = 1;     // offset in hours from UTC to your timezone
    var lab = 'cd';  // id of the entry on the page where the counter is to be inserted

    function start() 
	{
		displayCountdown(setCountdown(month,day,hour,min,tz),lab);
	}
    loaded(lab,start);

    // Countdown Javascript
    // copyright 20th April 2005, 1st November 2009 by Stephen Chapman
    // permission to use this Javascript on your web page is granted
    // provided that all of the code in this script (including these
    // comments) is used without any alteration
    // you may change the start function if required
    	var pageLoaded = 0; 
	window.onload = function() {pageLoaded = 1;}

   	 function loaded(i,f) 
	{
		if (document.getElementById && document.getElementById(i) != null) f();
		 else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
    	}


	function setCountdown(month,day,hour,min,tz) 
	{
		var m = month;
		if (month=='*') m = 0;
		var c = setC(m,day,hour,tz);
		if (month == '*' && c < 0)
			c = setC('*',day,hour,tz); return c;
	}

	function setC(month,day,hour,tz) 
	{
		var toDate = new Date();
		if (day.substr(0,1) == '+') 
		{
			var day1 = parseInt(day.substr(1));
			toDate.setDate(toDate.getDate()+day1);
		} 
		else
		{
			toDate.setDate(day);
		}

		if (month == '*')
			toDate.setMonth(toDate.getMonth() + 1);
		else if (month > 0) 
		{
			if (month <= toDate.getMonth())
				toDate.setFullYear(toDate.getFullYear() + 1);
			toDate.setMonth(month-1);
		}
    		if (dow >0)
			toDate.setDate(toDate.getDate()+(dow-1-toDate.getDay())%7);


    		toDate.setHours(hour);
		toDate.setMinutes(min-(tz*60));
		toDate.setSeconds(0);
		
		var fromDate = new Date();
		fromDate.setMinutes(fromDate.getMinutes() + fromDate.getTimezoneOffset());
		
		var diffDate = new Date(0);
		diffDate.setMilliseconds(toDate - fromDate);

		return Math.floor(diffDate.valueOf()/1000);

	}
	


	function displayCountdown(countdn,cd) 
	{
		if (countdn < 0)
		{
			document.getElementById(cd).innerHTML = "The session for today has started.";
			//insert cycle here?
			//have 'cooldown' run for 12 hours. Then reinitiate the countdown.
			//looks like countdn is stored in seconds. Ergo..

			var cooldown = 12 * 60 * 60;
			while(cooldown > 0)
			{
				cooldown--;
				//need a pause for a second here
				
			}

			//recalc - DO NOT RESET MANUALLY. Instruction lag will mean we're offset despite the pause.

			//TODO - extend by one week, accepting month increase etc.

			//TODO - reinit start() with new values
			
			//TODO - rewrite the entire thing to avoid recursive displayCountdown calls
			//- current method explained about will cause start initialised within previous displayCountdown call :(

			
		}
		else 
		{
			var secs = countdn % 60;
			if (secs < 10) secs = '0'+secs;
			var countdn1 = (countdn - secs) / 60;
			var mins = countdn1 % 60;
			if (mins < 10)
				mins = '0'+mins;countdn1 = (countdn1 - mins) / 60;

			var hours = countdn1 % 24;
			var days = (countdn1 - hours) / 24;
			document.getElementById(cd).innerHTML = days+'d '+hours+'h '+mins+'m '+secs;

			setTimeout('displayCountdown('+(countdn-1)+',\''+cd+'\');',999);
		}
	}

User avatar
Xiathorn
Posts: 67
Joined: Fri Jun 04, 2010 3:04 pm

Re: Countdown

Post by Xiathorn » Wed Sep 08, 2010 9:46 am

In theory, this should work. I have no way of testing it, unfortunately. Fer : try this out, if it doesn't work then I'll have to actually grab a test enviroment and write it properly.

This doesn't have the capability to handle seconds as of yet. It will do as soon as I can get the time to do this properly, but I'm currently on holiday/job hunting and I'm not really in a position to write something flawless and perfect :P

I know we've got quite a few folks in folk (harr!) who are good at this sort of thing. I don't ever write JavaScript so I have no real clue if what I've done here is correct. It makes sense from a Java/C++ perspective, but who knows? Not me :)

There may be a couple of syntax errors, so be warned.

Code: Select all

//config section
var day = 0; // (0 - Sunday, 6 - Saturday)
var hour = 21; // (0-23)
var minute = 30; // (0-60)
var cooldown = 180; // (Time in minutes until restart of next cycle
var cycle = 1; // (0,1) if we're cycling indefinitely or just a one-off

function start()
       {
          xStart(lab);
       }
        loaded(lab,start);

window.onload = function() {pageLoaded = 1;}

           function loaded(i,f)
       {
          if (document.getElementById && document.getElementById(i) != null) f();
           else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
           }



function xStart(cd)
{
	//outer loop

	while(true)
	{

	//inner loop #1

	while(true)
	{
		//get the current time
		var date = GetDate();
		var curr_day = date.getDay();
		var curr_hour = date.getHours();
		var curr_minute = date.getMinutes();

		//get the distance between today and our target, taking into account the Sunday-start array
		var tDay = day - curr_day;
		tDay = Math.floor(tDay, 7);
		tDay = abs(tDay);

		//if < 0 then take answer, add to 24, and that's the number of hours. Subtract one day(?)
		var tHour = hour - curr_hour;
		if(tHour < 0)
		{
			tHour = 24 + tHour;
			tDay = tDay - 1;
		}

		var tMinute = minute - curr_minute;

		if(tMinute < 0)
		{
			tMinute = 60 + tMinute;
			tHour = tHour - 1;
		}

	
		//Detect if we have reached the correct time - we have a minute-long window, so no need to synch at the low level

		if((tDay == 0) && (tHour == 0) && (tMinute == 0))
		{
			//display the text saying the session has started
			document.getElementById(cd).innerHTML = "The session for today has started";

			//break the loop
			break;
		}

		else
		{
			//display the text showing the time to session
			document.getElementById(cd).innerHTML = tDays+'d '+tHours+'h '+tMinutes+'m ';
		}

	} //end of inner loop #1

	//if we've met the length of the session, restart.
	while(true)
	{

		var date = GetDate();
		var curr_hour = date.getHours();
		var curr_minute = date.getMinutes();
		
		var tHours = cooldown / 60;
		var tMinutes = cooldown % 60;

		if((curr_hour == (hour + tHours)) && (curr_minute == (minutes + tMinutes)))
		{
			break;
		}
	} //end of inner loop #2

		
	
	
	} //end of outer loop
} //end of function

	

Post Reply