• I'm working on a project which basically does 2 jobs, which both have different outputs depending on the current time of day:

    1. continuously watch pin for sensor data, and perform function A whenever it arrives
    2. perform function B on fixed schedule (e.g. every minute)

    I currently have a setwatch() for 1 and a function start() for 2. But both require the same function for checking the time of day and doing different things depending on it.

    What's the best way to structure this code? I'm happy doing either one, but I feel like I'm missing something in my approach to combine them.

    Sorry if I've not explained this well - thought it would be better as an abstract question, than sharing my mess of code.

  • Fri 2019.07.05

    Hello @user101594,

    'What's the best way to structure this code?'

    Too many possibilities for anyone to just guess. The tutorial, a Javascript language syntax issue, understanding functions in general, referencing a function, Time library issue, timing?

    It is difficult to explain a process to solve an issue, without a foundation on which to start. As difficult as posting your code might seem, it will be by far the most efficient.

    Please see "Writing an effective forum post"


    for a few other items to help us better assist your situation. Also add links to the tutorials that have been attempted.

    'both require the same function'

    Lets start with the above referenced 'same function' and what has been attempted so far.

  • I'm not sure I can be much help either, except to say that IMO it's a good idea to re-use as much code as possible. If you've got a function you're using in both places, definitely keep that.

    Also, for anything more than a few lines it's worth using a named function rather than just inserting it into a setWatch/setInterval. eg.:

    function onSecond() {
    setInterval(onSecond, 1000);
    //rather than:
    setInterval(function() {
    ...}, 1000);

    As it allows you toreplace the function in running code if you're developing and want to tweak something.

    But I'm not convinced that really answers your question :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Best way to structure code for combining continuous pin watch with repeating function?

Posted by Avatar for user101594 @user101594