JavaScript setinterval delay one second solution

JavaScript setinterval delay one second solution

When using setinterval, it is found that it will be executed after a delay of one second when the page is just opened. Because the setinterval timer executes its own one second first, and then operates on the content inside, it will not be displayed immediately.

For example: first create a div box, then write the script code

var div = document.querySelector('div');
			var num = 10;
			setInterval(function(){
				if(num==1){
					div.innerHTML = null;
					return fn1;
				}else{
					num--;
					div.innerHTML = 'Remaining' + num + 'seconds';
				}
			},1000);

The effect is as shown below:

It will execute for that second first, wait for one second and then execute the content displayed in it

Solution:

Direct call

var div = document.querySelector('div');
			var num = 11;
			function fn1(){
				if(num==1){
					div.innerHTML = null;
					return fn1;
				}else{
					num--;
					div.innerHTML = 'Remaining' + num + 'seconds';
				}
			}
			setInterval(fn1,1000);
			fn1();

This is the end of this article about the solution to the one-second delay of JavaScript setinterval. For more information about the solution to the one-second delay of JavaScript setinterval, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Common array operations in JavaScript
  • A simple and in-depth study of async and await in JavaScript
  • JavaScript implementation of classic snake game
  • Javascript basics about built-in objects
  • Detailed explanation of JavaScript operation mechanism and a brief discussion on Event Loop
  • Comparison between Python coroutines and JavaScript coroutines
  • JavaScript to achieve mouse tailing effect
  • JavaScript to achieve simple drag effect
  • Detailed explanation of JavaScript array deduplication
  • JavaScript to implement the aircraft war game
  • Detailed explanation of JavaScript upload file limit parameter case
  • A brief talk about JavaScript variable promotion
  • In-depth understanding of JavaScript event execution mechanism
  • 8 essential JavaScript code snippets for your project

<<:  Basic statements of MySQL data definition language DDL

>>:  Tomcat maxPostSize setting implementation process analysis

Recommend

Quickly install MySQL5.7 compressed package on Windows

This article shares with you how to install the M...

Detailed explanation of the specific use of the ENV instruction in Dockerfile

1. The ENV instruction in the Dockerfile is used ...

Tutorial diagram of installing CentOS and Qt in Vmware virtual machine

Vmware Installation Installing Packages Download ...

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the I...

Specific use of Node.js package manager npm

Table of contents Purpose npm init and package.js...

Font references and transition effects outside the system

Copy code The code is as follows: <span style=...

MySQL data types full analysis

Data Type: The basic rules that define what data ...

Detailed explanation of the JVM series memory model

Table of contents 1. Memory model and runtime dat...

Example of making a butterfly flapping its wings with pure CSS3

Pure CSS3 makes a butterfly flapping its wings, s...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...

Nginx sample code for implementing dynamic and static separation

In combination with the scenario in this article,...

Best Practices for Implementing Simple Jira Projects with React+TS

A set of projects for training react+ts Although ...

Introduction to Computed Properties in Vue

Table of contents 1. What is a calculated propert...