跳至正文
版本:latest - a1.147.x ⚖️

⏱️ 计时器

在指定的时间间隔执行代码。


🗿Static Class
This is a Static Class. Access it's methods directly with .. It's not possible to spawn new instances.
💂Authority
This static class can be accessed on both 🟧 Client and 🟦 Server side.
🧑‍💻API Source
This page is auto-generated! The Functions, Properties and Events described here are defined in our GitHub's API Repository! Feel free to commit suggestions and changes to the source .json API files!

信息

最短的可能间隔等于本地刻速率(Tick Rate)——通常为 33ms。 在服务器端,该数值可能会根据 Config.toml 的设置而有所不同。

🎒 示例

-- 创建一个间隔,每 2 秒调用一次函数
local my_interval = Timer.SetInterval(function(param1, param2)
Console.Log("每 2 秒触发一次!Param1:" .. param1 .. "。Param2:" .. param2)
end, 2000, "awesome param 1", 456)

-- 取消间隔
Timer.ClearInterval(my_interval)

-- 创建一个超时,在 5 秒后调用 my_function 一次 - 传递一个参数
Timer.SetTimeout(function(my_param)
Console.Log("nanos " .. my_param)
end, 5000, "world")
(Timer.Bind) Binding a Timer to an Entity
local character = Character(Vector(), Rotator(), "nanos-world::SK_Male")

local my_timer = Timer.SetTimeout(function(_character)
-- Do something with _character
-- Ex: Destroy the character after 10 seconds
_character:Destroy()
end, 10000, character)

-- Binds the Timer to the Character
-- This will ensure it will never trigger if the character is destroyed before it
-- With this you don't need to validate if the '_character' parameter is valid
Timer.Bind(my_timer, character)

🗿 静态函数

ReturnsNameDescription
Bind将一个计时器绑定到任何 Actor。当该 Actor 被销毁时,计时器将自动被清除
ClearInterval停止执行在 SetInterval() 中指定的函数
ClearTimeout停止执行在 SetTimeout() 中指定的函数
integerGetElapsedTime返回自上一个刻以来已过去的时间
integerGetRemainingTime返回距离下一个刻所剩余的时间
booleanIsValid检查一个计时器当前是否处于活动状态或正在等待触发
Pause暂停计时器
ResetElapsedTime重置计时器以使其重新从头开始
Resume恢复计时器
integerSetInterval与 SetTimeout() 相同,但会连续循环地重复执行该函数
SetRemainingTime设置距离下一个刻所剩余的时间
integerSetTimeoutExecutes a function, after waiting a specified number of milliseconds

Bind

将一个计时器绑定到任何 Actor。当该 Actor 被销毁时,计时器将自动被清除
Timer.Bind(timer_id, actor)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID
Base Actoractor Required parameter 要绑定的 Actor
Timer.Bind Examples
Binding a Timer to an Entity
local character = Character(Vector(), Rotator(), "nanos-world::SK_Male")

local my_timer = Timer.SetTimeout(function(_character)
-- Do something with _character
-- Ex: Destroy the character after 10 seconds
_character:Destroy()
end, 10000, character)

-- Binds the Timer to the Character
-- This will ensure it will never trigger if the character is destroyed before it
-- With this you don't need to validate if the '_character' parameter is valid
Timer.Bind(my_timer, character)

ClearInterval

停止执行在 SetInterval() 中指定的函数
Timer.ClearInterval(interval_id)

Parameters

TypeParameterDefaultDescription
integerinterval_id Required parameter SetInterval() 返回的 ID 值用作此方法的参数

ClearTimeout

停止执行在 SetTimeout() 中指定的函数
Timer.ClearTimeout(timeout_id)

Parameters

TypeParameterDefaultDescription
integertimeout_id Required parameter SetTimeout() 返回的 ID 值用作此方法的参数

GetElapsedTime

返回自上一个刻以来已过去的时间
local elapsed_time = Timer.GetElapsedTime(timer_id)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID

Returns

TypeDescription
integerelapsed_time

GetRemainingTime

返回距离下一个刻所剩余的时间
local remaining_time = Timer.GetRemainingTime(timer_id)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID

Returns

TypeDescription
integerremaining_time

See also SetRemainingTime.


IsValid

检查一个计时器当前是否处于活动状态或正在等待触发
local is_valid = Timer.IsValid(timer_id)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID

Returns

TypeDescription
booleanis_valid

Pause

暂停计时器
Timer.Pause(timer_id)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID

ResetElapsedTime

重置计时器以使其重新从头开始
Timer.ResetElapsedTime(timer_id)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID

Resume

恢复计时器
Timer.Resume(timer_id)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID

SetInterval

与 SetTimeout() 相同,但会连续循环地重复执行该函数
local ret = Timer.SetInterval(callback, milliseconds?, parameters...?)

Parameters

TypeParameterDefaultDescription
functioncallback Required parameter 将要执行的回调函数。
返回 false 以阻止其继续被调用。
integermilliseconds?0
计时器在每次执行指定函数之间需要延迟的时间(毫秒)
anyparameters...?nil
传递给该函数的额外参数

Returns

TypeDescription
integerinterval_id

SetRemainingTime

设置距离下一个刻所剩余的时间
Timer.SetRemainingTime(timer_id, time)

Parameters

TypeParameterDefaultDescription
integertimer_id Required parameter 计时器 ID
integertime Required parameter 剩余的时间(毫秒)

See also GetRemainingTime.


SetTimeout

Executes a function, after waiting a specified number of milliseconds
local ret = Timer.SetTimeout(callback, milliseconds?, parameters...?)

Parameters

TypeParameterDefaultDescription
functioncallback Required parameter The callback that will be executed
integermilliseconds?0
The time in milliseconds to wait before executing the function
anyparameters...?nil
传递给该函数的额外参数

Returns

TypeDescription
integerthe timeout_id

🚀 事件

This class doesn't have own events.