LUA延时控制

与C++交互实现

通过C++提供Sleep函数给LUA调用即可,可以任意实现。比如超过5秒再提示相应倒计时功能。

if(funName == L"Sleep") {
	if(lua->CheckParamCount(params, 1)) {
		int sleepMs = lua->ParseInt(THelper::GetLuaParamAt(params, 0));
		DWORD startMoment = GetTickCount();
		int second = 0, total = sleepMs / 1000, lastSecond = 0;
		while(GetTickCount() < startMoment + sleepMs) {
			second = (GetTickCount() - startMoment) / 1000;
			if(sleepMs > 5000 && (second != lastSecond)) {
				lua->LogInfo(THelper::FormatString(L"倒计时: %d 秒", total - second));
				lastSecond = second;
			}
			THelper::Util::Delay(100);
		}
	}
}

如果还希望达到毫秒级控制,用C++也表示毫无压力

其它可参考方式

网上资源比较多。如

Lua封装延时执行函数

https://blog.csdn.net/zhenyu5211314/article/details/50437901
延时执行函数

function delayTimeGuideEvent( target, func, times )
    -- 延迟时间执行函数
    local delaytime = 1
    if times then delaytime = times end
    getRoot(target):runAction(CCSequence:createWithTwoActions(CCDelayTime:create(delaytime), CCCallFunc:create(func)))
end

LUA的延时调用功能

https://blog.csdn.net/yejian2011/article/details/41173031

-- 延时 0.2 关闭 

    local schedulerEntry = nil

    local    scheduler = cc.Director:getInstance():getScheduler()

    local function removeLayer(time)

        scheduler:unscheduleScriptEntry(schedulerEntry)

        schedulerEntry = nil

        layer:removeFromParentAndCleanup(true)   

    end

    schedulerEntry = scheduler:scheduleScriptFunc(removeLayer, 0.2, false)

Sleep Function

http://lua-users.org/wiki/SleepFunction

A common need is to pause (sleep) a program for a certain number of seconds, preferably without busy waiting.
This function to do this without busy waiting does not exist in ANSI C, so it does not exist in stock Lua. However, there are extension libraries and calls to external programs that can do this.

Solution: Busy Wait
local clock = os.clock
function sleep(n) – seconds
local t0 = clock()
while clock() - t0 <= n do end
end
– warning: clock can eventually wrap around for sufficiently large n
– (whose value is platform dependent). Even for n == 1, clock() - t0
– might become negative on the second that clock wraps.
Solution: C extension
There is a sleep function in ExtensionProposal. This may call Win32 Sleep or POSIX usleep. Here’s a [usleep/sleep C wrapper] example.
The LuaApr binding has an [apr.sleep()] function that works on Windows & UNIX and supports sub-second resolution.
The lalarm library[1] can set an alarm on POSIX.
winapi (Windows only) has a [sleep] function. [github]
If an FFI interface (Alien or c/invoke – BindingCodeToLua) is available, you can call whichever OS function you have.
Solution: sleep command
function sleep(n)
os.execute("sleep " … tonumber(n))
end
Windows does not have such a built-in command. However, there’s a sleep in the Windows Server Resource Kit. There is also sleep in Cygwin and MinGW. Also, there is “timeout” utility available in Windows 7
os.execute("timeout " … tonumber(n)) – specific to win7 (and probably higher)
Solution: ping or other programs
function sleep(n)
if n > 0 then os.execute(“ping -n " … tonumber(n+1) … " localhost > NUL”) end
end
– version 20100715 - fixed off-by-one second
This is mainly for Windows in the absence of a sleep command. Other variations exist, e.g. “perl -e 'sleep(” … tonumber(n) … “)’” or “php -r sleep(” … tonumber(n) … “);”.
Solution: I/O wait
io.stdin:read’*l’
This is not a sleep but may be useful in similar cases. It waits for the user to press the Enter key.
Solution: Using WScript (Windows)
function sleep(n)
local vb = “test.vbs”
local f = assert(io.open(vb,“w”))
f:write(“WScript.Sleep(” … (tonumber(n) * 1000) … “)\n”)
f:close()
os.execute(vb)
end
See [2].
Solution: sleep()
The POSIX sleep() call provides integer second sleeps.
require “posix”
posix.sleep(3)
Solution: socket.sleep()
The LuaSocket? module provides a sleep function.
socket = require(“socket”)
function sleep(sec)
socket.sleep(sec)
end
sleep(0.2)
Solution: ngx.sleep()
Nginx Lua module provides a sleep function. One can specify time resolution up to 0.001 seconds (i.e., one milliseconds). Behind the scene, this method makes use of the Nginx timers.
ngx.sleep(sec)
Solution: lsocket.select()
The select() timeout provides a fairly portable sub-second sleep, if you can tolerate the socket library dependency.
local lsocket = require(“lsocket”)
function sleep(sec)
lsocket.select(sec)
end
sleep(2)
Solution: LuaJIT FFI/LuaFFI
local ffi = require “ffi”
ffi.cdef “unsigned int sleep(unsigned int seconds);”
ffi.C.sleep(2)
Solution: os.time()
function sleep(s)
local ntime = os.time() + s
repeat until os.time() > ntime
end
Solution: os.clock()
. .
Using the os.clock() method instead of os.time(), you can get precision down to one 100th of a second while os.time() only allows intervals based on the timestamp, which at execution can be at anything from 0.1 to 1 second. The os.time() method is great for longer periods over 2 seconds where precision isn’t that much of a deal.

function sleep(s)
local ntime = os.clock() + s/10
repeat until os.clock() > ntime
end

没细品,后续再研究

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值