View Single Post
12-31-14, 07:12 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Dandamis View Post
What I found helpful in my one day experience of making Addons:
Code:
local last = 0;
frame:SetScript("OnUpdate", function(self, elapsed)
	last = last + elapsed;
	
	if last >= 1 then
		-- Your code
		last = 0;
	end
end)
OnUpdate polling is generally the worst (ie. most computationally expensive) way to accomplish anything. If there's an event that fires to tell you when your code should do something, you should always use an event handler instead instead. And since 6.0 there is now an actual timer API built into the game, so if you need to repeat something every 1 second as in your example code, you should do this instead:

Code:
local function Update()
     -- your code

    -- Tell it to repeat again in 1 second:
    C_Timer.After(1, Update)
end

-- Start it:
Update()
(There's also C_Timer.NewTimer and C_Timer.NewTicker methods that automate the repeating for you, but they're implemented in Lua, not in C, and are rather inefficient in typical Blizzard fashion, so it's better to just handle it yourself.)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote