[PHP]local f = CreateFrame("frame")
f:SetScript("OnUpdate", function(self, elapsed)
self.elapsed = self.elapsed + elapsed
if self.elapsed >= 5 then
self.elapsed = 0
DoStuff()
end
end)
f.elapsed = 0
[/PHP]
is about as simple as it gets.
To elaborate, an OnUpdate script runs for every frame draw (your fps) while the frame with the OnUpdate script is shown. If you want to stop the script, you hide your frame.
For something running every frame draw wouldn't a local be better than indexing 3 to 4 times each call? Something like:
local frame = CreateFrame('Frame')
do
local timer = 0
frame:SetScript('OnUpdate', function(self, elapsed)
timer = timer + elapsed
if timer >= 5 then
timer = 0
DoStuff()
end
end)
end
What is the most efficient way to create a timer? I need to run a piece of code every five seconds.
f:SetScript("OnUpdate", function(self, elapsed)
self.elapsed = self.elapsed + elapsed
if self.elapsed >= 5 then
self.elapsed = 0
DoStuff()
end
end)
f.elapsed = 0
[/PHP]
is about as simple as it gets.
http://www.wowwiki.com/Using_OnUpdate_correctly
Thanks anyway.
yeah, i know what you mean -- seems like a lot of overhead, but in the scheme of things, it's probably not really that bad.
Just make sure your code behaves :p