Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Timer = advancedMacros.inherit()
- Timer. __class = "com.theincgi.Timer"
- local _new = Timer.new
- function Timer:new( sFunc )
- local obj = _new( self )
- if( sFunc == nil )then error("Missing function") end
- obj.func = sFunc
- obj.count = -1
- obj.initDelay = 0
- obj.loopDelay = 0
- obj.exit = false
- obj.isPause = false
- obj.thread = thread.new(function()
- sleep( obj.initDelay )
- local n = 0
- while not obj.exit do
- if obj.count~=-1 and n==obj.count+1 then
- break
- end
- if not obj.isPause then
- --sFunc()
- a,b = pcall(sFunc)
- if not a then
- error("Error occured inside timer function\n"..b)
- end
- end
- n = n+1
- sleep( obj.loopDelay )
- end
- end)
- return obj
- end
- function Timer:setInitDelay( millis )
- assert(self, "Self argument is invalid")
- self.initDelay = millis
- end
- function Timer:setLoopDelay( millis )
- assert(self, "Self argument is invalid")
- self.loopDelay = millis
- end
- function Timer:setLoopCount( times )
- assert(self, "Self argument is invalid")
- self.count = times
- end
- function Timer:start()
- assert(self, "Self argument is invalid")
- self.thread.start()
- end
- function Timer:pause()
- assert(self, "Self argument is invalid")
- self.isPause = true
- end
- function Timer:unpause()
- assert(self, "Self argument is invalid")
- self.isPause = false
- end
- function Timer:stop()
- assert(self, "Self argument is invalid")
- self.exit = true
- end
- function Timer:kill()
- assert(self, "Self argument is invalid")
- self.thread.stop()
- end
- function Timer:className()
- assert(self, "Self argument is invalid")
- return self.__class
- end
- return Timer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement