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

How to automatically deploy Linux system using PXE

Table of contents Background Configuring DHCP Edi...

Summary of 6 solutions for implementing singleton mode in JS

Preface Today, I was reviewing the creational pat...

Definition and usage of MySQL cursor

Creating a Cursor First, create a data table in M...

How to parse the attribute interface of adding file system in Linux or Android

The first one: 1. Add key header files: #include ...

How to use Linux whatis command

01. Command Overview The whatis command searches ...

Detailed explanation of computed properties in Vue

Table of contents Interpolation Expressions metho...

Implementation of services in docker accessing host services

Table of contents 1. Scenario 2. Solution 3. Conc...

Detailed explanation on how to modify the default port of nginx

First find out where the configuration file is wh...

Zabbix3.4 method to monitor mongodb database status

Mongodb has a db.serverStatus() command, which ca...

Application example tutorial of key in Vue page rendering

introduction During the front-end project develop...

Example code for converting Mysql query result set into JSON data

Mysql converts query result set into JSON data Pr...