计时器使用服务未被拨打的服务

发布于 2025-02-10 03:26:45 字数 1636 浏览 1 评论 0原文

我已经在组件中创建了一个Angular以用于计时器的服务文件

  import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    export class TimerUtilService {
    
      initial_module_timer_value = 300;
      second_left:any;
      module_finish_interval;
      time_limit_over:any=false;
    
      constructor() { }
      start_countdown()
      { 
        this.startTimer(this.initial_module_timer_value);
      }
      startTimer(duration) {
        var timer = duration, minutes, seconds;
        var self = this;
        this.module_finish_interval= setInterval(() => { 
          minutes = (timer / 60) | 0;
          seconds = (timer % 60) | 0;
          if(timer>=0)
          {
          minutes = minutes < 10 ? "0" + minutes : minutes;
          seconds = seconds < 10 ? "0" + seconds : seconds;
          this.second_left = minutes + ":" + seconds;
        
          }
     if(--timer < 0)
    {
    console.log('completed');
    }
          },1000);
      }
      end_countdown()
      {  
      clearInterval(this.module_finish_interval);
      }
    }

,我想调用此服务

import { TimerUtilService } from '../../../shared/timer-util.service';

export class TestComponent implements OnInit {
constructor(private route: ActivatedRoute,private elem: ElementRef,private router: Router,private http: HttpClient,private timerService : TimerUtilService) {
    this.timerService.start_countdown();
    console.log(this.timerService.second_left);
}

在控制台中获取无效值。

我想在我的组件中显示计时器,例如05:00分钟,那么该计时器应在调用的组件中每秒减小,该组件被调用,直到剩下的时间为00:00秒。

谢谢

I have created a service file in angular for timer

  import { Injectable } from '@angular/core';
    
    @Injectable({
      providedIn: 'root'
    })
    export class TimerUtilService {
    
      initial_module_timer_value = 300;
      second_left:any;
      module_finish_interval;
      time_limit_over:any=false;
    
      constructor() { }
      start_countdown()
      { 
        this.startTimer(this.initial_module_timer_value);
      }
      startTimer(duration) {
        var timer = duration, minutes, seconds;
        var self = this;
        this.module_finish_interval= setInterval(() => { 
          minutes = (timer / 60) | 0;
          seconds = (timer % 60) | 0;
          if(timer>=0)
          {
          minutes = minutes < 10 ? "0" + minutes : minutes;
          seconds = seconds < 10 ? "0" + seconds : seconds;
          this.second_left = minutes + ":" + seconds;
        
          }
     if(--timer < 0)
    {
    console.log('completed');
    }
          },1000);
      }
      end_countdown()
      {  
      clearInterval(this.module_finish_interval);
      }
    }

In my component i want to call this service

import { TimerUtilService } from '../../../shared/timer-util.service';

export class TestComponent implements OnInit {
constructor(private route: ActivatedRoute,private elem: ElementRef,private router: Router,private http: HttpClient,private timerService : TimerUtilService) {
    this.timerService.start_countdown();
    console.log(this.timerService.second_left);
}

Getting null value in console.

I want to show timer in my component like if 05:00 minutes then this timer should decrease every second in the component called in which it is called till time left is 00:00 seconds.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ζ澈沫 2025-02-17 03:26:45

您正在获得null结果,因为second_left变量在调用console.log(this.timerservice.second_left)时仍然不确定。

尝试在服务功能中放置一个console.log,然后查看幕后发生的事情:

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class TimerUtilService {

  initial_module_timer_value = 300;
  second_left:any;
  module_finish_interval;
  time_limit_over:any=false;

  constructor() { }
  start_countdown()
  { 
    this.startTimer(this.initial_module_timer_value);
  }
  startTimer(duration) {
    var timer = duration, minutes, seconds;
    var self = this;
    this.module_finish_interval= setInterval(() => { 

      //##################
      // Logging timer and seconds at every interval
      //#################
      console.log("Timer: " + timer);
      console.log("Seconds left: " + second_left);        

      minutes = (timer / 60) | 0;
      seconds = (timer % 60) | 0;
      if(timer>=0)
      {
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;
      this.second_left = minutes + ":" + seconds;
      if(timer==0)
      {
        console.log('completed');
      }  
      }
      },1000);
  }
  end_countdown()
  {  
  clearInterval(this.module_finish_interval);
  }
}

You're getting a null result because the second_left variable is still undefined while invoking the console.log(this.timerService.second_left); .

Try putting a console.log in the service function and see what is happening behind the scenes like:

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class TimerUtilService {

  initial_module_timer_value = 300;
  second_left:any;
  module_finish_interval;
  time_limit_over:any=false;

  constructor() { }
  start_countdown()
  { 
    this.startTimer(this.initial_module_timer_value);
  }
  startTimer(duration) {
    var timer = duration, minutes, seconds;
    var self = this;
    this.module_finish_interval= setInterval(() => { 

      //##################
      // Logging timer and seconds at every interval
      //#################
      console.log("Timer: " + timer);
      console.log("Seconds left: " + second_left);        

      minutes = (timer / 60) | 0;
      seconds = (timer % 60) | 0;
      if(timer>=0)
      {
      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;
      this.second_left = minutes + ":" + seconds;
      if(timer==0)
      {
        console.log('completed');
      }  
      }
      },1000);
  }
  end_countdown()
  {  
  clearInterval(this.module_finish_interval);
  }
}
清音悠歌 2025-02-17 03:26:45

您可能需要考虑将要启动的功能放在ngoninit()函数中,因此

ngOnInit(): void {
 this.timerService.start_countdown();
}

请尝试使用startTimer()函数,

var timer = duration, minutes, seconds;
var x = setInterval(() => { 
    minutes = (timer / 60) | 0;
    seconds = (timer % 60) | 0;
    if(timer>=0){
        timer=timer-1
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        var second_left = minutes + ":" + seconds;

        console.log(second_left);

        if(timer==0){
            console.log("cleared");
            clearInterval(x)
        }
    }
},1000)

因为构造函数仅调用一次,您只会有一个00:00秒秒钟的控制台log。

编辑

一种方法是在testomponent.ts本身中使用startTimer(),而不是timerutilservice,如果您想使用服务,则需要可观察的方式 ,但是这种方式可能会引起性能问题,而不是很大的想法持续的反馈。

You might want to consider putting the functions you want to init in to ngOnInit() function like so

ngOnInit(): void {
 this.timerService.start_countdown();
}

Try this in side your startTimer() function,

var timer = duration, minutes, seconds;
var x = setInterval(() => { 
    minutes = (timer / 60) | 0;
    seconds = (timer % 60) | 0;
    if(timer>=0){
        timer=timer-1
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        var second_left = minutes + ":" + seconds;

        console.log(second_left);

        if(timer==0){
            console.log("cleared");
            clearInterval(x)
        }
    }
},1000)

As the constructor only call once , you will only have one 00:00 seconds console log.

Edited

One way is to use the startTimer() in the TestComponent.ts itself not the TimerUtilService , if u want to use a service you will need the observable way but this way may cause performance issues and not very idea for constant feedback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文