Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

How to replace fibaro:sleep with setTimeout in a loop


Question

Posted

 Many of my scenes have this structure where I am iterating through an array and processing each entry, and I want a delay between each.  I have always used fibaro:sleep.

 

I am struggling to find a good way to use setTimeout here.  Surely if I call Process_Entry with setTimeout, the loop is going to carry on running.

 

Please login or register to see this code.

 

5 answers to this question

Recommended Posts

  • 0
Posted (edited)

Another approach is to generalise it to a function that process each item in a table/sequence with a delay, and then an option interval when it should start over.

The sequenceProcessor function returns a reference that can be used to stop the processing.

Please login or register to see this code.

 

Edited by jgab
  • Thanks 1
  • 0
Posted (edited)

Please login or register to see this code.

something like this. You must make sure that all entries are processed within the 'Delay' time, or the jobs will start to stack up.

So, what we do here is that in the Main_Loop we fire off all the setTimeout at once but spaced in time with n*(Breather_Secs*1000).

 

There are more advanced ways to do this that chain the setTimeout calls, but that would require a more drastic change to your program structure...

Edited by jgab
  • 0
Posted (edited)

The more advanced could look like this - no risks that jobs are stacking up.
 

Please login or register to see this code.

 

Edited by jgab
  • 0
  • Inquirer
  • Posted

    The arrays range from 10s to 100s of cells, so I don't want to have that many timers.

     

    I did not know that next existed, thanks.

    • 0
    Posted

    Well, starting 100-1000 timers in parallell is not a problem

    The problem is if you start them faster then they complete so they start to stack up - as it will of course eat memory.

    However, timers don't eat cpu while they wait to execute, so that is at least good news.

     

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...