Thursday 3 August 2023

Javascript: How to print numbers from 1 to 10 using javascript setInterval?

 Javascript: How to print numbers from 1 to 10 using javascript setInterval?


Solution: This is basic interview questions.  

setInterval() method is used to call a function or evaluate an expression at specified intervals (in milliseconds). For example, let's log a message after 1 seconds using setInterval method,

setInterval(function () {
  console.log("Hello Javascript");
}, 1000);
Solution for above Question is:
let i =1;
let basicint = setInterval(function () {
  console.log("My count is:",i);
    i++;
    if(i>10){
        clearInterval(basicint); // Here clearing the interval after 10 iterations
    }
}, 1000);


Finally the out is:


Quick Solutions

More

About Us

Hello!
I`m Sharath.
I'll design & develop dynamic websites,web applications, and anything related to web. I'll develop projects in JAVA,.Net, PHP and Android Apps .

Contact Us

Sharath Babu
Company : SLS Tech Services.
URL : SLS Tech Services
Top