html - Refresh javascript variables without refreshing the page - Stack Overflow

So I have a variable that is constantly changing (num) and it's controlled by other page, but to g

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

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

Share Improve this question edited Jun 20, 2018 at 15:21 Miguel asked Jun 20, 2018 at 15:17 MiguelMiguel 1631 gold badge3 silver badges10 bronze badges 6
  • 1 Unclear what you want but Ajax is the thing to use to retrieve data from another resource in the background. – Alex K. 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? – Grzegorz J 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 – ADyson 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. – yeshashah 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 – Ossaija Thankgod Commented Jun 20, 2018 at 15:32
 |  Show 1 more ment

1 Answer 1

Reset to default 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"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744208425a4563217.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信