How to expose variable from php to js - and keep it updated?
You have two options, depending on how responsive you need it to be, and how complex you're willing to get:

1) You can add Javascript to your page that will continually send AJAX requests to the server in the background, essentially asking for the value of that variable. You would achieve this by creating a new menu path with the sole purpose of serving the value of that variable in JSON. The JS that sits on the page could then be set up to ask for that variable every X seconds until it changes (or it could just keep doing it forever if you wanted).

2) You could write a separate Node.js application that runs on your server, which listens for updates to the variable from the background task  and then pushes a notification to the JS in your page via websockets. That's obviously a lot more complex. But potentially more performant, and the updates would be instant. See this module for an example of a Node.js chat application: https://www.drupal.org/project/nodejs