Counting down to 11pm using Javascript.

Once again I found myself pondering another use for Javascript to help make my work more easier and to address what is perhaps my biggest concern during my shift. How much longer before I can go home?

I have seen tons of sample scripts that count down to a specific date, which is nice but I wanted to create a script that counts down to a specific time instead. In my case I wanted the script to count down to 11pm, which marks the end of my shift. I also wanted to be able to run this same script each day without having to change anything in the code, as I noticed in the date countdown scripts I saw.

So. Off I went to find the script. After some searching and sweating I came across this discussion forum in which a fellow Javascript novice needed some help creating a countdown script similar to what I was looking for. The moderator responded by posting a countdown script, one that I tried out and liked well enough for me to experiment with to get it to do my bidding.

Here’s my variation on the forum moderator’s script which counts down to 11pm:

<html>
<head>
<title>End of Shift Countdown</title>
</head>
<body>
<script type=”text/javascript”>
// From: http://www.webdeveloper.com/forum/showthread.php?t=215504
// Modified for: http://www.webdeveloper.com/forum/showthread.php?t=220174
var definedTime = new Date(’23:00′); // military time to countdown TO
function ShowTimes() {
var now = new Date();
var hrs = 22-now.getHours();
if (hrs < 0) { hrs += 24; }
var mins = 60-now.getMinutes();
var secs = 60-now.getSeconds();
var str = ”;
str += ‘<center><font size=”7″><br>’+hrs+’ hours, ‘+mins+’ minutes to go</font></center>’;
document.getElementById(‘countdownToMidnight’).innerHTML = str;
}

var _cntDown;
function StopTimes() { clearInterval(_cntDown); }
</script>
</head>
<body onload=”_cntDown=setInterval(‘ShowTimes()’,1000)”>
<div id=”countdownToMidnight”></div>
</body>
</html>

The biggest change I made to the script was modifying the “var hrs” line to subtract 22 from the getHours() variable. I cleaned up the output and did away with displaying the seconds as all I really wanted to do was just count down the hours and minutes to 11pm.

I still consider myself a total novice when it comes to Javascript but it’s nice to know the web is full of sample scripts to help me along.

Specific word counter for Javascript.

At my job where I do Internet tech support, I use Notepad to keep a log of the day’s calls. The entries that fill this log were generated by a database system that lets me pull up the customer’s account so I can document what happened during the call. These entries always start with the word “Agent” so by counting the number of times that word appears in my call notes, I can tally up how many calls I get.

One way to do the word count was loading Microsoft Word, pasting the call notes and then executing the Find command. Word would then tell me how many instances of the word “Agent” it found, but the process of loading such a large program like Word and navigating my way to the Find command seems a bit time-consuming. Surely there must be an easier, faster way to do this.

Then I found a web site with a handy word frequency counter that lists the words and the number of times they appear in the pasted text. No offense to WriteWords but it doesn’t feel right transmitting call notes full of customer phone numbers across the Internet to an unsecured web site just to find out how many calls I got.

I gave some thought to creating a simple HTML document using Javascript to count the number of occurences of the word “Agent” but didn’t know how to go about it since I am a total novice at Javascript. A search on the almighty Google found tons of scripts that count the total number of words in a block of text but I had a harder time finding a script to just count the number of instances of a single word.

Then I found this script which has the interface I’m looking for. I want to be able to paste my call notes into a text field and click a Submit button and have the script return the grand total and thought this was a good script to use.

I studied the script closely and took notice of the line that says:

words = str.split(” “);

I may not be familiar with Javascript but I am familiar with how programming languages work with strings, and no doubt this is a command to split the string into separate substrings wherever there’s a space between them. I got an idea and added the word “Agent”, thus:

words = str.split(“Agent”);

And it works. I paste my call notes in the input box, click the button and the script works its magic to give me the number of calls I got that day. I then made some cosmetic changes to the script to customize the text accordingly.

Here’s my modified script:

<HTML>
<HEAD>
<TITLE>Call Counter</TITLE>
<HEAD>
<SCRIPT>
function countWords(str){
var count = 0;
words = str.split(“Agent”);
for (i=0 ; i < words.length ; i++){
// inner loop — do the count
if (words[i] != “”)
count += 1;
}

document.theForm.results.value =
“You got ” +
count +
” calls.”;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name=”theForm”>
Paste your call notes here.<p>
<TEXTAREA name=inStr rows=5 cols=90>
</TEXTAREA>
<p>
<INPUT type=button value=”Count Calls”

onClick=”countWords(document.theForm.inStr.value)”;>
<p>
<TEXTAREA name=results rows=1 cols=20>
</TEXTAREA>
</FORM>
</BODY>
</HTML>

Another dilemma solved by JavaScript.

Thanks to Java2S for posting the tutorial.