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

Detailed tutorial on installing Nginx 1.16.0 under Linux

Because I have been tinkering with Linux recently...

Problems with creating placeholders for HTML selection boxes

I'm using a placeholder in a text input and i...

CSS 3.0 text hover jump special effects code

Here is a text hovering and jumping effect implem...

How to manually scroll logs in Linux system

Log rotation is a very common function on Linux s...

Learn the common methods and techniques in JS arrays and become a master

Table of contents splice() Method join() Method r...

CSS implements Google Material Design text input box style (recommended)

Hello everyone, today I want to share with you ho...

Sample code for implementing interface signature with Vue+Springboot

1. Implementation ideas The purpose of interface ...

CSS method of controlling element height from bottom to top and from top to bottom

Let’s start the discussion from a common question...

Three ways to communicate between React components (simple and easy to use)

Table of contents 1. Parent-child component commu...

A brief introduction to VUE uni-app basic components

1. scroll-view When using vertical scrolling, you...

Javascript to achieve the drag effect of the login box

This article shares the specific code of Javascri...

Detailed explanation of MySql installation and login

Check if MySQL is already installed in Linux sudo...

Solution to the garbled code problem in MySQL 5.x

MySQL is a commonly used open source database sof...

How to configure Hexo and GitHub to bind a custom domain name under Windows 10

Hexo binds a custom domain name to GitHub under W...