• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Run JavaScript if a file is created

Scheduled Pinned Locked Moved General Discussion
25 Posts 3 Posters 12.2k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    EoF
    last edited by Jul 28, 2016, 5:43 PM

    Might be wise to incorporate some AJAX into this, no? Having PHP check the file, etc and then pass the results / message back to JS.

    1 Reply Last reply Reply Quote 0
    • K Offline
      KirAsh4 Moderator
      last edited by Jul 28, 2016, 6:30 PM

      Not necessarily:
      MM² module --> node_helper --> php --> node_helper --> MM² module

      A Life? Cool! Where can I download one of those from?

      E 1 Reply Last reply Jul 29, 2016, 1:56 PM Reply Quote 0
      • O Offline
        ooom416354 @KirAsh4
        last edited by Jul 28, 2016, 8:11 PM

        @KirAsh4 This is awesome!

        I would just put this in my JS file, right? I’m currently sending the following command through Tasker to the Pi so I think I’m good.

        touch curr-train.txt

        1 Reply Last reply Reply Quote 0
        • K Offline
          KirAsh4 Moderator
          last edited by Jul 28, 2016, 8:14 PM

          Earlier you mentioned you using PHP, so I kept it in the loop. Use node_helper to call the PHP script. Let the PHP script check for the file and report back to node_helper which in turn reports to the module.

          A Life? Cool! Where can I download one of those from?

          O 2 Replies Last reply Jul 28, 2016, 8:32 PM Reply Quote 0
          • O Offline
            ooom416354 @KirAsh4
            last edited by Jul 28, 2016, 8:32 PM

            @KirAsh4 OK, so my PHP script is actually index.php, it’s effectively the page that loads and is my Mirror. In that file I have a reference to the JS file which runs the mta code.

            <script src="js/mm-mta.js"></script>

            So would I put the following in the PHP page? (sorry if I sound dumb here)

            var prev_datestamp = 0;
            var expire_time = 15 * 60 * 1000; // 15 minutes
            
            // run every five minutes
            if (file exists) {
              if (file.datestamp > previous_datestamp) {
            
               <script src="js/mm-mta.js"></script>
            
                previous_datestamp = file.datestamp;
              } else if (now > file.datestamp + expire_time) {
            
              <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
              <script> 
                  $("#mta").empty();
              </script>
            }
            }
            1 Reply Last reply Reply Quote 0
            • O Offline
              ooom416354 @KirAsh4
              last edited by Jul 28, 2016, 8:43 PM

              @KirAsh4 for reference here’s the full index.php file

              index.php

              1 Reply Last reply Reply Quote 0
              • K Offline
                KirAsh4 Moderator
                last edited by Jul 28, 2016, 8:46 PM

                Oy, why do it that way … your index.php will never get updated if there’s an upstream change in the code (because we use index.html). You should use the existing infrastructure in MM². Create a new module, have it display in a specific region on the mirror. Every 5 minutes, have the module refresh by doing a call to its node_helper. The node_helper can be a simply python script that checks the specific file in question and reports back to the module.

                I thought the PHP script was doing something … it’s not in this case. You don’t need PHP at all for this.

                Some places to look for coding ideas:
                MM² Module Development Documentation
                Look at how modules such as the calendar and newsfeed implement a node_helper which does the background tasks

                You could still use PHP to do the file checking for you, but to me that’s a bit of an unnecessary overhead.

                A Life? Cool! Where can I download one of those from?

                O 2 Replies Last reply Jul 28, 2016, 8:50 PM Reply Quote 0
                • O Offline
                  ooom416354 @KirAsh4
                  last edited by Jul 28, 2016, 8:50 PM

                  @KirAsh4 LOL - I’m just lost these days with all of this stuff. I should stick to my DB work.

                  Let me see what I can do with the module documentation, it would be a good experience anyway to learn.

                  1 Reply Last reply Reply Quote 0
                  • O Offline
                    ooom416354 @KirAsh4
                    last edited by Jul 28, 2016, 8:54 PM

                    @KirAsh4 Just out of curiosity, if I were to add the code you provided into the PHP file, would i need to bracket it with <?php then add the code here?>

                    1 Reply Last reply Reply Quote 0
                    • K Offline
                      KirAsh4 Moderator
                      last edited by Jul 28, 2016, 9:18 PM

                      This is hard to answer because I don’t know what your PHP file looks like. I don’t know if you are echo-ing HTML data, or using HEREDOC? I’m flying blind here … (as much fun as that could be)

                      A Life? Cool! Where can I download one of those from?

                      O 1 Reply Last reply Jul 28, 2016, 11:25 PM Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 2 / 3
                      2 / 3
                      • First post
                        11/25
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy