How to count 30sec after bool flag=true

 

Hi, I just want to ask for your useful idea.

If flag becomes true, I just want to count 30sec,

After 30sec, then I want to delete object.

I tried to use Sleep(30000), but this sleep time stop other program in EA, so I don't want to use it.

Do you have any good idea ?


bool flag;

If (some event occure) flag=true,

...
After 30sec,  ObjectDelete(   0,   "OBJECTA" );
 
Hong Ling Mu:

Hi, I just want to ask for your useful idea.
If flag becomes true, I just want to count 30sec,
After 30sec, then I want to delete object.
I tried to use Sleep(30000), but this sleep time stop other program in EA, so I don't want to use it.
Do you have any good idea ?


Hi, 

you can use the OnTimer() function 
https://docs.mql4.com/eventfunctions/eventsettimer

Event Handling Functions - Functions - Language Basics - MQL4 Reference
Event Handling Functions - Functions - Language Basics - MQL4 Reference
  • docs.mql4.com
Event Handling Functions - Functions - Language Basics - MQL4 Reference
 
Hong Ling Mu: If flag becomes true, I just want to count 30sec,

After 30sec, then I want to delete object.

When you set the flag, remember the time. On a new tick, compute the duration.

 
Yohana Parmi #:


Hi, 

you can use the OnTimer() function 
https://docs.mql4.com/eventfunctions/eventsettimer

Thank you!

I will study OnTimer function.

 
Hong Ling Mu #:

Thank you!

I will study OnTimer function.

if(TimeCurrent()>wait_until)
{
        // 30 seconds expired
}

// set condition to wait for 30 sec from now
wait_until = TimeCurrent()+30;
 

If you are not using the timer for other things then you can do this: 

bool flag;

if (some event occure && !flag) 
        {
        flag=true;
        EventSetTimer(30);
        }

void OnTimer()
{
ObjectDelete(   0,   "OBJECTA" );
EventKillTimer();
}
 
Laszlo Tormasi #: If you are not using the timer for other things then you can do this: 

That may not work. OP wants a 30-second delay. Enabling the timer does not mean the first call will be 30 seconds later.

Reason: