Search code examples
javascriptphphtmldmx512

How to pass PHP/html to javascript loop


I have a pretty robust JavaScript script that controls my room lights via some external software. I have a PHP that when run returns only a 1 or a 0. I ideally want to have a JavaScript loop that runs every couple seconds and pulls that 1 or 0 and if it is a 1, do some work. Problem is, can't figure out how to get the JavaScript to open another page in terms or processing/data collection.

Is there a "file get contents" equal to JavaScript? I don't want the PHP values written on the JavaScript page, just to have the value to do a little if statement.


Solution

  • As @Marc B said, you want to make AJAX calls. Here is a idea in how to implement it using jQuery.

    <script>
        function updateData(){
          $.get( "ajax/test.html", function( data ) {
            //now "data" has the response 
          });
        }
    
        setInterval(updateData, updateInterval);
    </script>
    

    JavaScript Timers - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Timers

    jQuery $.get() - http://api.jquery.com/jQuery.get/