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
Because I have been tinkering with Linux recently...
I'm using a placeholder in a text input and i...
Sometimes the page is very long and needs an arro...
Here is a text hovering and jumping effect implem...
Log rotation is a very common function on Linux s...
Table of contents splice() Method join() Method r...
Hello everyone, today I want to share with you ho...
1. Implementation ideas The purpose of interface ...
Let’s start the discussion from a common question...
Table of contents 1. Parent-child component commu...
1. scroll-view When using vertical scrolling, you...
This article shares the specific code of Javascri...
Check if MySQL is already installed in Linux sudo...
MySQL is a commonly used open source database sof...
Hexo binds a custom domain name to GitHub under W...