How to run a piece of code once in a while

Hi,
I was looking for a way to run a piece of code once in a while, and accidentally found the answer in a seemingly unrelated guide here: AFL Function Reference - SAY

To quote from that guide:

function SayNotTooOften( text, Minperiod )
{
   elapsed=GetPerformanceCounter()/1000;
   Lastelapsed = Nz( StaticVarGet("lastsaytime") );

  if( elapsed - Lastelapsed > Minperiod )
   {
     StaticVarSet("lastsaytime", elapsed );
    
     Say( text );
   }
  
}

Instead of Say(), another piece of code can be run once in a while using this method, which is what I need.
I started this topic so other users looking for the same solution could find it faster.
Thank you Tomasz for the great work!

3 Likes

you are great
thank you

I don't know if it is good idea to use this for anything else than Say() because original code was intended for one and only one purpose: make computer not to Say() too much. It was desired to SKIP some sayings and that is what this code does. For the user who asked for it, it was irrelevant if he is missing some text. In other applications it might not be the case.

1 Like

Hi Tomasz,
I appreciate your vision for perfection and efficiency, which drives the great development of AmiBroker, and I'm sure you have more elegant solutions to the issues we're facing.
I understand that the thread title and content might not be up to the standards, yet my need was solved with an adaptation of this code, hence this thread to help someone like me save some time.
Thanks again

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.