Timer.h library : executing a function every X seconds IF a condition is met

I have a question about the Timer library : I tried the examples and they work, except if I want to add a condition.

I want a function to be executed every 8 seconds IF a condition is true. I see that in all examples the line

t.every(8000,doSomething);

is always in the setup.

But if the line is in the setup, how can I execute it with a condition in the loop ? I tried putting the in the loop but it looks like the function is called by series of 10 times very fast, with a 8 seconds interval...

Thanks for any help !

edit : mistake in the code

noobuinoo:
But if the line is in the setup, how can I execute it with a condition in the loop ? I tried putting the in the loop but it looks like the function is called by series of 10 times very fast, with a 8 seconds interval...

the library attaches a function (doSomething) to an interrupt which is triggered every 8000ms in your example. That is how it works...

what is it that you wish to do?

As previously explained, that line of code in the Setup causes the designated function to be called every 8 seconds.

The easy solution is to put the if condition in the function. The function will still be called every 8 seconds, but will only do whatever it is doing if the condition evaluates to true.

A slightly more complicated solution is to Change the line in the Setup. That libary call is designed to give you an internal id number (unsigned Long). Once you have that ID number, it can be used together with another library call to stop(id). Then, the function is no longer executed every 8 seconds. The stop function basically also kills the Event so to restart it, use the same line as is being used in the Setup now. Within your sketch, test your if condition and stop/restart the timer Event appropriately.