From 9adbccd2f878ef6c69600c0f25443c0b196a128b Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 25 Nov 2021 00:56:07 +1100 Subject: [PATCH] feat(init): implement interval This is different from the one implemented in Ox ESX. --- init.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/init.lua b/init.lua index dd19c0e..d39a354 100644 --- a/init.lua +++ b/init.lua @@ -35,6 +35,37 @@ function import(file) return IMPORTS[file] end +----------------------------------------------------------------------------------------------- +-- Global functions +----------------------------------------------------------------------------------------------- + +--- Dream of a world where this PR gets accepted. +--- ```lua +--- SetInterval(callback: function, timer: number) +--- ``` +SetInterval = setmetatable({currentId = 0}, { + __call = function(self, callback, timer) + local id = self.currentId + 1 + self.currentId = id + self[id] = timer or 0 + CreateThread(function() + repeat + local interval = self[id] + Wait(interval) + callback(interval) + until interval == -1 + self[id] = nil + end) + return id + end +}) + +function ClearInterval(id) + if SetInterval[id] then + SetInterval[id] = -1 + end +end + -- pe-lualib -- Copyright (C) 2021 Linden