有点误会

2024-04-26 18:58:38 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我如何在代码中多次使用python的类计时器吗。在

import MOD

class timer:
    def __init__(self, seconds):
         self.start(seconds)
    def start(self, seconds):
         self.startTime = MOD.secCounter()
         self.expirationTime = self.startTime + seconds
         if seconds != 0:
            self.running = 1
            self.expired = 0
         else:  
            self.running = 0  
            self.expired = 0
    def stop(self):
         self.running = 0
         self.expired = 0
    def isexpired(self):
         if self.running == 1:  
            timeNow = MOD.secCounter()  
            if timeNow > self.expirationTime:    
               self.running = 0    
               self.expired = 1  
            else:    
               self.expired = 0
         return self.expired
    def isrunning(self):
         if self.running == 1:  
             timeNow = MOD.secCounter()  
             if timeNow > self.expirationTime:    
                self.running = 0    
                self.expired = 1  
             else:    
                self.expired = 0
         return self.running
    def change(self, seconds):
         self.expirationTime = self.startTime + seconds
    def count(self):
         if self.running == 1:  
            timeNow = MOD.secCounter()  
            return (timeNow - self.startTime)
         else:  
            return -1

他们写下这样的评论:

下面是一个关于如何使用此类的简单示例:

^{pr2}$

但我不知道如何在代码中多次使用它,因为我需要在代码中使用多个计时器

我应该写信吗

    timerB = timers.timer(1)
    timerB.start(1800)
    while 1:  
        if timerB.isexpired():    
        print 'timerA expired'  
        break

有什么帮助吗


Tags: 代码selfmodreturnifdefstartrunning
1条回答
网友
1楼 · 发布于 2024-04-26 18:58:38

结束-参数定时器计时器开始计时的秒数。但每次你打电话定时器(),您将获得一个新的计时器实例。在

所以你的代码看起来更像:

  timerB = timers.timer(1800)
    while 1:  
        if timerB.isexpired():    
        print 'timerA expired'  
        break

除了这是误导-timerA和timerB是独立的计时器,所以timerB.isexpired()不会告诉您有关timerA的任何信息。也许你是说“timerB过期了”?在

我也建议不要这么快就进行投票。每次检查后也许睡一会儿?在

相关问题 更多 >