Another JS countdown script

Here’s a simple one I came up with. It does a countdown until a certain date. Quite simple.

Hope you can find use for it:

var CDOWN = {
 init : function()
  {
   this.dformat = "[D]:[H]:[M]:[S]"; // Day:Hour:Minute:Second - Any can be removed
   this.tdate = "11/19/2008 6:00 PM"; // Until when to countdown (MM/DD/YYYY)
   this.count = true; // Update the counter each second or minute or whatever is defined above
   this.endstr = "Live now!"; // To display once the countdown is complete
   this.lzero = true; // Display leading zeros on numbers lower than 10
   this.counts = -1;

   this.counts = Math.ceil(this.counts);
   if(this.counts == 0)
     this.count = false;

   this.timeoutp = (Math.abs(this.counts)-1)*1000 + 990;

   dthen = new Date(this.tdate);
   dnow = new Date();

   ddiff = (this.counts > 0) ? new Date(dnow-dthen) : new Date(dthen-dnow);

   gsecs = Math.floor(ddiff.valueOf()/1000);
   CDOWN.startCounter(gsecs);
 },

calculate : function(secs, num1, num2)
 {
   s = ((Math.floor(secs/num1))%num2).toString();
   if(this.lzero && s.length < 2)
     s = "0" + s;
   return s;
 },

startCounter : function(secs)
 {
   if(secs < 0)
    {
     document.getElementById("timer").innerHTML = this.endstr;
     return;
    }

    this.dsp = this.dformat.replace(/\[D\]/g, CDOWN.calculate(secs,86400,100000));
    this.dsp = this.dsp.replace(/\[H\]/g, CDOWN.calculate(secs,3600,24));
    this.dsp = this.dsp.replace(/\[M\]/g, CDOWN.calculate(secs,60,60));
    this.dsp = this.dsp.replace(/\[S\]/g, CDOWN.calculate(secs,1,60));

    document.getElementById('timer').innerHTML = this.dsp;
    if(this.count)
      setTimeout("CDOWN.startCounter(" + (secs+this.counts) + ")", this.timeoutp);
 }
}

/* LOAD */
function addLoadEvent(fn)
 {
   var old = window.onload;
   if(typeof window.onload != 'function')
     window.onload = fn;
   else
     window.onload = function()
      {
        old();
        fn();
      }
 }

addLoadEvent(function() { CDOWN.init(); });

In order to display this inside some container, you will need to define one and set it’s ID to be the same as in the script. For example:

document.getElementById('timer').innerHTML = this.dsp;

In this case my container’s id is “timer” and the actual countdown will be displayed there.

Mappeo.com has been dropped

The mappeo.com project has been dropped due to lack of time and I’ve decided to release the code under GNU/GPL licence and it’s free for download.

The code is not complete but it’s at a very advanced stage and you shall quickly grasp the go-along of it.

Requires:

  • PHP 5 (With GD enabled)
  • MySQL 5+
  • A browser with JS enabled and AJAX support
  • Apache webserver (With mod_rewrite enabled)
  • A Google API key

More: http://mappeo.com

C string inverter

Some friend of mine just asked me to write him a simple string inverter in C, meaning a little program which reads a string and outputs the same string in inverted order. For example:

Input: Hello, we are done!
Output: !enod era ew ,olleH

I honestly don’t know the use of it, but any way here is it. (Not tested, but it should work fine)

Read more to see the code or download it zipped.

I just wrote a quick & dirty PHP version as well. Download.

However, the PHP version could be simplified a lot (one line of code):

<?php
    echo strrev("Hello, we are done!");
?>

Here’s the C code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct list
 {
	char item;
	struct list *link;
 };

typedef struct list x;
	x *stack, *temp = NULL;

/* Pushes characters to the stack */
push(char data)
 {
	stack = (x *)malloc(sizeof(x));
	stack->item = data;
	stack->link = temp;
	temp = stack;
 }

/* Pops characters from the stack */
pop()
 {
	x *t;
	while(temp != NULL)
	 {
		printf("%c", temp->item);
		t = temp;
		temp = temp->link;
		free(t);
	 }
	printf(" ");
	temp = NULL;
 }

main()
 {
	int i;
	char string[30];
	clrscr();
	printf("\n Your text: ");
	gets(string);
	i = 0;
	printf("\n Inverted: ");
	while(string[i] != '\0')
	 {
		(isspace(string[i]) != 0) ? pop() : push(string[i]);
		i++;
	 }
	pop();
 }