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
1 Answer
Reset to default 4I 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条)