Sign in to follow this  
IgorRodriguesCo

getTime condition

Recommended Posts

How I run the line only once, until the condition is true again?

timehour, timeminute = getTime()
var = 0

if timehour == 12 and timeminute == 0 then
  var = var + 1 --I want to execute this line only once whenever the condition is true
end

Apparently the line runs more than once during the time of the condition.

Share this post


Link to post

Nope it will run a couple of times if it's either inside a loop or gets called again within a short amount of time the best thing to do is add some kind of boolean variable.

Example: 

  1. timehour, timeminute = getTime()
  2. debounce = false
  3. var = 0
  4.  
  5. if timehour == 12 and timeminute == 0 and debounce == false then
    1. var = var + 1 --I want to execute this line only once whenever the condition is true
    2. debounce = true
  6. end

Hope this helps!

 

  • Thanks 1

Share this post


Link to post

Please remove that background color, or whatever is that, because it's ugly like this.
So.
He haven't said that it's in a loop, he just gave us a code with an 'if' statement.

Share this post


Link to post

Well exactly you have figured out the problem, if it's not in a loop it's being called multiple times. One thing you should really learn to do is brighten up that attitude.

 

 

Edited by Crossy101
  • Like 1

Share this post


Link to post
3 hours ago, Crossy101 said:

Nope it will run a couple of times if it's either inside a loop or gets called again within a short amount of time the best thing to do is add some kind of boolean variable.

Example: 

  1. timehour, timeminute = getTime()
  2. debounce = false
  3. var = 0
  4.  
  5. if timehour == 12 and timeminute == 0 and debounce == false then
    1. var = var + 1 --I want to execute this line only once whenever the condition is true
    2. debounce = true
  6. end

Hope this helps!

 

 

Thank you! By your logic, Ive added another conditional so that the line is executed again when it reaches the condition. Look:

timehour, timeminute = getTime()
debounce = false
var = 0

if timehour == 12 and timeminute == 0 and debounce == false then
  var = var + 1 --I want to execute this line only once whenever the condition is true
  debounce = true
elseif timehour ~= 12 and timeminute ~= 0 and debounce == true then
  debounce = false
end

 

Incredibly just with 'else', I went back to the previous problem. Determining exactly the conditions, as I did by your logic, it works perfectly!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.