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

I specialize in designing and developing dynamic websites, web applications, and other web-related projects. My expertise includes working with Java, .NET, PHP, and developing Android apps.

Contact Us

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