找回密码
马上加入

QQ登录

只需一步,快速开始

搜索
发新帖

0

收听

2

听众

108

主题

lua 节流函数

IP属地:浙江省杭州市
发表于 2024-7-6 20:08:58 | 查看: 429| 回复: 7

在 Lua 中,实现节流函数(Throttle Function)可以限制某个函数在特定时间间隔内只执行一次。以下是一个简单的 Lua 节流函数实现示例:

-- 节流函数
function throttle(func, interval)
    local lastTime = 0
    return function(...)
        local currentTime = os.time()
        if currentTime - lastTime >= interval then
            lastTime = currentTime
            func(...)
        end
    end
end

-- 示例函数
function exampleFunction()
    print("Function executed!")
end

-- 创建一个节流函数,间隔2秒
local throttledFunction = throttle(exampleFunction, 2)

-- 调用节流函数
throttledFunction()
throttledFunction()
throttledFunction()

在这个实现中:

  1. throttle 函数接收两个参数:func(需要节流的函数)和 interval(时间间隔,单位为秒)。
  2. 返回一个新的函数,该函数会记录上一次执行的时间,并在当前时间与上一次执行时间的间隔大于或等于 interval 时才执行 func
  3. 使用 os.time() 获取当前时间,以秒为单位。

这样,通过 throttle 函数,你可以有效地限制某个函数在特定时间间隔内只执行一次,避免频繁触发。

发表于 2024-11-25 19:03:43 IP属地:河南省濮阳市

| | 学到了 每天一学 快速成长 | | --------------------------- |

回复 显示全部楼层 道具 举报

发表于 2024-10-31 11:48:09 IP属地:湖北省武汉市
学到了  每天一学 快速成长

回复 显示全部楼层 道具 举报

发表于 2024-8-18 23:20:44 IP属地:浙江省杭州市

[quote][size=2][color=#999999]a159909252a 发表于 2024-8-8 20:20[/color][/size] 服务端节流的话能设置毫秒级么 ostime 最少只能 1 秒 [/quote]

在 Lua 中,os.time() 的精度确实只能达到秒级,如果你想要毫秒级的节流功能,可以使用 socket.gettime() 或者 os.clock(),它们提供了更高精度的时间测量方法。

以下是使用 socket.gettime() 实现毫秒级别的节流函数:

-- 引入socket库
local socket = require("socket")

-- 节流函数
function throttle(func, interval)
    local lastTime = 0
    return function(...)
        local currentTime = socket.gettime() * 1000 -- 将秒转换为毫秒
        if currentTime - lastTime >= interval then
            lastTime = currentTime
            func(...)
        end
    end
end

-- 示例函数
function exampleFunction()
    print("Function executed at " .. socket.gettime() * 1000 .. "ms")
end

-- 创建一个节流函数,间隔500毫秒
local throttledFunction = throttle(exampleFunction, 500)

-- 调用节流函数
throttledFunction()
throttledFunction()
throttledFunction()

在这个例子中,我们使用了 socket.gettime() 来获取当前时间,并将其转换为毫秒,以实现更精确的时间间隔控制。这样,你就可以实现毫秒级别的节流控制。

回复 显示全部楼层 道具 举报

发表于 2024-8-8 20:20:41 IP属地:浙江省杭州市

服务端节流的话能设置毫秒级么 ostime 最少只能 1 秒

回复 显示全部楼层 道具 举报

发表于 2024-7-9 14:45:51 IP属地:四川省成都市

有用,可以限制到前端功能请求服务端的次数

回复 显示全部楼层 道具 举报

发表于 2024-7-9 09:38:49 IP属地:广东省深圳市

ALG NIU BI

回复 显示全部楼层 道具 举报

发表于 2024-7-6 20:10:13 IP属地:浙江省杭州市

你可以使用 SL:ScheduleOnce(callback, time) 来替换 os.time() 实现节流函数。以下是修改后的代码:

-- 节流函数
function throttle(func, interval)
    local isThrottled = false
    return function(...)
        local args = {...}
        if not isThrottled then
            isThrottled = true
            func(unpack(args))
            SL:ScheduleOnce(function()
                isThrottled = false
            end, interval)
        end
    end
end

-- 示例函数
function exampleFunction()
    print("Function executed!")
end

-- 创建一个节流函数,间隔2秒
local throttledFunction = throttle(exampleFunction, 2)

-- 调用节流函数
throttledFunction()
throttledFunction()
throttledFunction()

在这个实现中:

  1. isThrottled 用于标记函数是否处于节流状态。
  2. 当函数被调用时,如果 isThrottledfalse,则执行函数并设置 isThrottledtrue
  3. 使用 SL:ScheduleOnce 设置一个定时器,在 interval 秒后将 isThrottled 重置为 false,允许函数再次执行。

这样,通过 SL:ScheduleOnce,你可以实现基于时间间隔的节流功能。

回复 显示全部楼层 道具 举报

您需要登录后才可以回帖 登录 | 马上加入

QQ|Archiver|手机版|小黑屋|alg阿灵戈社区 ( 苏ICP备2023026137号-1|苏ICP备2023026137号-1 )

GMT+8, 2025-3-13 01:05 , Processed in 1.516817 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表