In Matlab functions can be started at events,but occasionally, like with the resize function, the events are called in rapid order and the function is called many times in succession, which can cause weird behavior and lag. Is there a way to have it listen for the event but only execute on the last event in a time range, e.g. .5 second?
I tried using a persistent variable that each one would update and it would only run if the variable still equaled what they set it to after .5 seconds, but this didn't work. Are there any clean ways to do this in Matlab or any language that I can steal ideas from?
edit: For example here is an implementation of the persistent variable method that I tried:
function practice a = uipanel('ResizeFcn',@Delay,'Units','Normalized'); uicontrol(a) end function Delay(s,cb) persistent a if isempty(a) a = 0; end a = a+1; b = a; pause(.1); if b~=a %disp(a-b) else %do work here end end
This method doesn't seem to work all the time, but that might just be because of the use of the magic number in pause(). It is also very unclear as to what it does without deep reading.