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:
|
<<: Basic statements of MySQL data definition language DDL
>>: Tomcat maxPostSize setting implementation process analysis
Table of contents Background Configuring DHCP Edi...
When programmers do TypeScript/JavaScript develop...
Preface Today, I was reviewing the creational pat...
Creating a Cursor First, create a data table in M...
The first one: 1. Add key header files: #include ...
01. Command Overview The whatis command searches ...
Table of contents Interpolation Expressions metho...
Table of contents 1. Scenario 2. Solution 3. Conc...
This article uses examples to illustrate the prin...
First find out where the configuration file is wh...
Mongodb has a db.serverStatus() command, which ca...
introduction During the front-end project develop...
Mysql converts query result set into JSON data Pr...
Recently, two accounts on the server were hacked ...
Table of contents vite Build Configuration vite.c...