As you have not mentioned any language preference, I am using python here.
import time
c = time.time()
if A>B:
state = True
''' Do the initial action for this state'''
elif A<B:
state = False
''' Do the initial action for this state'''
while True:
if (time.time() - c) >= 5:
c = time.time()
A = check_A() # some dummy function for illustration purpose
B = check_B() # some dummy function for illustration purpose
if A>B:
if not state:
state = True
''' Do something that you like '''
elif A<B:
if state:
state = False
''' Do something that you like '''
Here I have assumed that you do not want anything to happen when when A==B
. The logic here that unless the state changes there will be no action for that particular state.
If here you do not want your code to be continuously running then you can use time.sleep(SLEEP_TIME)
.
import time
if A>B:
state = True
''' Do the initial action for this state'''
elif A<B:
state = False
''' Do the initial action for this state'''
while True:
time.sleep(5)
A = check_A() # some dummy function for illustration purpose
B = check_B() # some dummy function for illustration purpose
if A>B:
if not state:
state = True
''' Do something that you like '''
elif A<B:
if state:
state = False
''' Do something that you like '''