Detailed explanation of JavaScript timer and button effect settings

Detailed explanation of JavaScript timer and button effect settings

Timer Effects:

 <div>
    <font id='timeCount' style='display:inline-block; font-size:72px;width:100px;text-align:right;'>0</font> &nbsp;&nbsp;&nbsp;
    //The width of the time value needs to be fixed to avoid the position of the next three pictures changing when the time value changes from 9 to 10 (and from 99 to 100). //However, font is an inline element and its width cannot be set, so font is changed to an inline block element display:inline-block
    <img src='start.png' class='imgBtn' onclick="start(this)" >
    <img src='suspend.png' class='imgBtn' onclick="suspend(this)">
    <img src='stop.png' class='imgBtn' onclick="stop(this)">
</div>
 .imgBtn{ 
    cursor:pointer; 
    width:25px;
    height:25px;
}
 var timerState=2; //0-start (timing in progress) 1-suspend (pause timing) 2-stop (stop timing)
var timerID; //Timer //Click the start button to call the function function start(obj){
	if(timerState==0) //If the current state is timing, this click will not work return;
	else
	{
		timerState=0;//The indicator is timing changeImgBtnState(); //Change the display effect of the button timerID=setInterval("f7()",500); //Start the timer }
}
function suspend(obj){
	if(timerState==1 || timerState==2)
		return; //If the current state is to pause or stop timing, this click will not work else
	{
		timerState=1; //Mark pause timing changeImgBtnState(); //Change the display effect of the button clearInterval(timerID); //Clear timer }
}
function stop(obj){
	if(timerState==2) //If the current state is to stop timing, this click will not work return;
	if(timerState==0) //If the current state is timing, clear the timer clearInterval(timerID); 
	document.getElementById('timeCount').innerHTML=0; //Reset the timer value timerState=2; //Stop the timer changeImgBtnState(); //Change the display effect of the button}
function f7()
{
	var i=document.getElementById('timeCount').innerHTML;
	document.getElementById('timeCount').innerHTML=parseInt(i)+1;
}
function changeImgBtnState(){
	var imgBtn = document.getElementsByClassName('imgBtn');
	for(var i=0;i<3;i++){
		imgBtnState(imgBtn[i],timerState!=i);
	}
}
function imgBtnState(obj,flag){
	if(flag==false) //The button is unavailable obj.style.cssText="border:1px solid black;width:15px;height:15px;padding:5px;";
	else
		obj.style.cssText="border:0px solid black;width:25px;height:25px;padding:0px;";
}

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Vue.js implements simple timer function
  • Implementing a simple timer in JavaScript
  • JS implements a stopwatch timer
  • JS uses setInterval timer to implement the 10-second challenge
  • JavaScript implements a good-looking stopwatch timer
  • JavaScript setInterval() vs setTimeout() Timers
  • js implements built-in timer

<<:  How to add vector icons to web font files in web page production

>>:  MySQL database master-slave replication and read-write separation

Recommend

Alibaba Cloud Server Domain Name Resolution Steps (Tutorial for Beginners)

For novices who have just started to build a webs...

Install Apache2.4+PHP7.0+MySQL5.7.16 on macOS Sierra

Although Mac systems come with PHP and Apache, so...

Docker-compose quickly builds steps for Docker private warehouse

Create docker-compose.yml and fill in the followi...

In-depth analysis of the Tomcat server of Centos 7 system

Table of contents 1. The origin of tomcat 1. Tomc...

Summary and analysis of commonly used Docker commands and examples

Table of contents 1. Container lifecycle manageme...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...

The process of SSH service based on key authentication in Linux system

As we all know, SSH is currently the most reliabl...

The perfect solution for MySql version problem sql_mode=only_full_group_by

1. Check sql_mode select @@sql_mode The queried v...

A complete list of meta tag settings for mobile devices

Preface When I was studying the front end before,...

Implementation of Nginx configuration of multi-port and multi-domain name access

To deploy multiple sites on a server, you need to...

Several ways to shut down Hyper-V service under Windows 10

When using VMware Workstation to open a virtual m...

Turn off the AutoComplete function in the input box

Now we can use an attribute of input called autoco...

Understanding and solutions of 1px line in mobile development

Reasons why the 1px line becomes thicker When wor...