2

So I have a variable that is constantly changing (num) and it's controlled by other page, but to get the value from other page to another, I need to refresh the page. I tried auto refresh, but I have a youtube video playing and it would stop every refresh. Is there any way to refresh the variables only? Or constantly doing the javascript of the file so the values are always being updated.

EDIT: I am not using any server side or server language. Just HTML and JS

6
  • 1
    Unclear what you want but Ajax is the thing to use to retrieve data from another resource in the background. Commented Jun 20, 2018 at 15:19
  • 1
    Where it is stored this variable? Change where? On some database on server? Something like for example likes update in real-time? Commented Jun 20, 2018 at 15:19
  • 1
    "to get the value from other page to another, I need to refresh the page" no you don't, you can use AJAX. Although if you're just loading it from another static HTML page, you'll have to parse that page and look for the value Commented Jun 20, 2018 at 15:23
  • You can create a channel like backbone Notifications and use listener on the channel to poll for the value. Commented Jun 20, 2018 at 15:27
  • If you familiar with react or vuejs you can do this easily with either of them, harder to do with vanilla javascript Commented Jun 20, 2018 at 15:32

1 Answer 1

4

I believe the function you are looking for is setInterval(). This function will call a child function every time a certain period of time passes. In a limited use case, you can register a variable in a script and also register an interval timer to update it periodically. Depending on your use case, you may need to also write some JS to bind the new value of the number to the DOM.

Here is an example of using the intervals. In this example, clicking the button once begins the timer. After that, the interval (1000ms) will fire an event automatically to update the x variable and bind the new value to the DOM.

<!DOCTYPE html>
<html>
<body>
<button id="eventBtn" onclick="myFunction()">X = 0</button>
<script>
var x = 0;
function myFunction() {
    setInterval(function(){ 
        x++;
        var btn = document.getElementById("eventBtn");
        btn.innerHTML = 'X = ' + x;
    }, 1000);
}
</script>
</body>
</html>

Interactive JS Fiddle for example

W3Schools - "Window setInterval() Method"

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. Worked like a charm.
Unless I misunderstood, the question asked about getting a value from another page, but this only gets and sets something from the same page
People misunderstood the question and started commenting about AJAX, which was a rabbit-hole. If you read it closer, the question mentions being able to update the value with a page refresh, so getting network data was not the core issue. The issue is executing JS in the background continuously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.