timer

Creating a Pool out Timer in Javascript / Typescript

If you are stuck in pooling the request and need a timer just use these simple line of code.

  poolOutTimer(poolTime: number) {
    let timeLeft:number = poolTime;
    var timerId = setInterval(countdown, 1000);
    function countdown() {
      if (timeLeft == 0) {
        clearTimeout(timerId);
        //do something here for stop..
        console.log("Time UP : ");

      } else {
        //do something here while in progress..
        console.log("Time Left : ", timeLeft);
        timeLeft--;
        if (timeLeft%5==0) {
          console.log("Make New request here..");
          
        } else {
          console.log("Request Under Process..");          
        }
      }
    }
  }

Just For typescript or if you are using ionic with dynamic loader, I created this to show message on loader

Here loading:any; is a variable declared for loader.

poolOutTimer(poolTime: number) {
    this.timeLeft = poolTime;
    this.timerId = setInterval(()=>this.countdown(), 1000);
    this.showLoading();
  
  }

  countdown() {
   
    if (this.timeLeft == 0) {
      clearTimeout(this.timerId);
      //do something here for stop..
        console.log("Time UP : ");
        this.loading.dismiss();

    } else {
      //do something here while in progress..
      console.log("Time Left : ", this.timeLeft);
      this.timeLeft--;
      if (this.timeLeft%5==0) {
        console.log("Make New request here..");
        this.loading.setContent("New request");
    
        
      } else {
        console.log("Request Under Process..");     
        this.loading.setContent("Under Process");
       
      }
    }
  }


  showLoading() {
    this.loading = this.loadingCtrl.create({
      content: this.constant.loadingString
    });
    this.loading.present();
  }

Author Profile

PREM KUMAR
PREM KUMAR
Tech Lover , Open Src Dev , Dotnet Core, Ionic, Xamarin, Node, Angular2, Python, Opencv, AI, ML, Robotics, SignalR, Iot, Love Music, Cooking, Girls, Innovation.
Like
Like Love Haha Wow Sad Angry
1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.