Creating a Timer In Javascript
Javascript has a lot of features which are often overlooked by web developers, and one such feature is the ability to have timed events. If you do not understand what that means do not worry because all will be explained in this tutorial.
Timing events allow you to write code which will execute within a set amount of time. As an example, say you wanted to display a clock on your web page. You could use javascripts' built in timing functions to make the time change every second or every minute.
Of course there are other uses for such functions, but we will stick with a simple timer which creates a dialog box with the words Hi There on it every 3 seconds, that way you can get a feel for how to work with timing.
To get started we are going to need a basic web page. Just create a blank page and add the following code in the script tags:
setInterval(function(){alert("Hi There")},3000);
Once you have copied the code above and saved it in-between the body tags of your web page, go ahead and try to view the page. If you copied the code exactly as it is you will get a popup dialog box every 3 seconds with the words Hi There written on it.
Now the code its self is pretty easy to understand. On line one we use a script tag to tell the browser it is about to execute some javascript and we have a closing script tag at the end of the code block to make sure our code is actually executed. On line two we use javascripts built in function setInterval and if you look closely you will see that it takes two parameters.
The first parameter is an anonymous function which actually creates the dialog box with our string of text. The second parameter is an integer which tells the browser how long it should wait before the code is executed again.
Time in any programming language is measured in milliseconds so as you can see from the code above 3 seconds is written as 3000, likewise if you wanted to make your script wait for 10 seconds you would write it as 10000.
There are a multitude of ways you can use the ability to time events in your code, and the best way to discover them is by experimenting. Do not be afraid to play around with the code and put it to another use.
Javascript got a bad reputation a few years back for other people misusing its ability to create pop-up windows, and most of us avoid websites with unnecessary pop-up's so use the code presented here only if it has a real world use, otherwise you run the risk of people clicking away from your web site and never coming back.
other blog entries
Course Directory [training on all levels]
- .NET Classes
- Agile/Scrum Classes
- Ajax Classes
- Android and iPhone Programming Classes
- Blaze Advisor Classes
- C Programming Classes
- C# Programming Classes
- C++ Programming Classes
- Cisco Classes
- Cloud Classes
- CompTIA Classes
- Crystal Reports Classes
- Design Patterns Classes
- DevOps Classes
- Foundations of Web Design & Web Authoring Classes
- Git, Jira, Wicket, Gradle, Tableau Classes
- IBM Classes
- Java Programming Classes
- JBoss Administration Classes
- JUnit, TDD, CPTC, Web Penetration Classes
- Linux Unix Classes
- Machine Learning Classes
- Microsoft Classes
- Microsoft Development Classes
- Microsoft SQL Server Classes
- Microsoft Team Foundation Server Classes
- Microsoft Windows Server Classes
- Oracle, MySQL, Cassandra, Hadoop Database Classes
- Perl Programming Classes
- Python Programming Classes
- Ruby Programming Classes
- Security Classes
- SharePoint Classes
- SOA Classes
- Tcl, Awk, Bash, Shell Classes
- UML Classes
- VMWare Classes
- Web Development Classes
- Web Services Classes
- Weblogic Administration Classes
- XML Classes
- Microsoft Azure AI Fundamentals (AI-900T00)
25 November, 2024 - 25 November, 2024 - Linux Fundaments GL120
9 December, 2024 - 13 December, 2024 - VMware vSphere 8.0 Boot Camp
9 December, 2024 - 13 December, 2024 - Introduction to C++ for Absolute Beginners
16 December, 2024 - 17 December, 2024 - Introduction to Spring 5 (2022)
16 December, 2024 - 18 December, 2024 - See our complete public course listing
did you know? HSG is one of the foremost training companies in the United States
Our courses focus on two areas: the most current and critical object-oriented and component based tools, technologies and languages; and the fundamentals of effective development methodology. Our programs are designed to deliver technology essentials while improving development staff productivity.
An experienced trainer and faculty member will identify the client's individual training requirements, then adapt and tailor the course appropriately. Our custom training solutions reduce time, risk and cost while keeping development teams motivated. The Hartmann Software Group's faculty consists of veteran software engineers, some of whom currently teach at several Colorado Universities. Our faculty's wealth of knowledge combined with their continued real world consulting experience enables us to produce more effective training programs to ensure our clients receive the highest quality and most relevant instruction available. Instruction is available at client locations or at various training facilities located in the metropolitan Denver area.
Upcoming Classes
- Microsoft Azure AI Fundamentals (AI-900T00)
25 November, 2024 - 25 November, 2024 - Linux Fundaments GL120
9 December, 2024 - 13 December, 2024 - VMware vSphere 8.0 Boot Camp
9 December, 2024 - 13 December, 2024 - Introduction to C++ for Absolute Beginners
16 December, 2024 - 17 December, 2024 - Introduction to Spring 5 (2022)
16 December, 2024 - 18 December, 2024 - See our complete public course listing
consulting services we do what we know ... write software
The coaching program integrates our course instruction with hands on software development practices. By employing XP (Extreme Programming) techniques, we teach students as follows:
Configure and integrate the needed development tools
MOntitor each students progress and offer feedback, perspective and alternatives when needed.
Establish an Action plan to yield a set of deliverables in order to guarantee productive learning.
Establish an Commit to a deliverable time line.
Hold each student accountable to a standard that is comparable to that of an engineer/project manager with at least one year's experience in the field.
These coaching cycles typically last 2-4 weeks in duration.
Business Rule isolation and integration for large scale systems using Blaze Advisor
Develop Java, .NET, Perl, Python, TCL and C++ related technologies for Web, Telephony, Transactional i.e. financial and a variety of other considerations.
Windows and Unix/Linux System Administration.
Application Server Administration, in particular, Weblogic, Oracle and JBoss.
Desperate application communication by way of Web Services (SOAP & Restful), RMI, EJBs, Sockets, HTTP, FTP and a number of other protocols.
Graphics Rich application development work i.e. fat clients and/or Web Clients to include graphic design
Performance improvement through code rewrites, code interpreter enhancements, inline and native code compilations and system alterations.
Mentoring of IT and Business Teams for quick and guaranteed expertise transfer.
Architect both small and large software development systems to include: Data Dictionaries, UML Diagrams, Software & Systems Selections and more