Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Add DelayRepeat function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Jan 6, 2020
1 parent 84304dc commit a1576c4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/ReplicatedStorage/Aero/Shared/Thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
so the delay can be cancelled by disconnecting
the returned connection.
DelayRepeat(Number intervalTime, Function func, Arguments...)
> The same as Thread.Delay, except it repeats
indefinitely.
> Returns the Heartbeat connection, thus the
repeated delay can be stopped by disconnecting
the returned connection.
> Properly bound to the time interval, thus will
not experience drift.
Examples:
Expand All @@ -59,6 +71,12 @@
end)
delayConnection:Disconnect()
local repeatConnection = Thread.DelayRepeat(1, function()
print("Hello again", tick())
end)
wait(5)
repeatConnection:Disconnect()
Why:
Expand Down Expand Up @@ -129,4 +147,18 @@ function Thread.Delay(waitTime, func, ...)
end


function Thread.DelayRepeat(intervalTime, func, ...)
local args = {...}
local nextExecuteTime = (tick() + intervalTime)
local hb
hb = heartbeat:Connect(function()
if (tick() >= nextExecuteTime) then
nextExecuteTime = (tick() + intervalTime)
func(table.unpack(args))
end
end)
return hb
end


return Thread

0 comments on commit a1576c4

Please sign in to comment.