简体   繁体   English

每当我的js数据文件发生变化时如何刷新页面

[英]How to refresh a page whenever my js data file changes

So I've got this local file named 'data.js' containing various data. 所以我有这个名为'data.js'的本地文件包含各种数据。

  var json={ 'city': 'madrid', 'zip':'21212', 'street':'bergstrasse', 'house':'15', }; for(key in json) { if(json.hasOwnProperty(key)) $('input[name='+key+']').val(json[key]); } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <form action="options.php" method="post" > <input type="text" name="city" placeholder="stadt" > <br> <input type="text" name="zip" placeholder="zip" > <br> <input type="text" name="street" placeholder="straße" > <br> <input type="text" name="house" placeholder="hausnummer" > <br> <input type="submit" value="speichern" name="saves"></input> </form> <script src="data.js"></script> 

I want to refresh my page only when some data in the js file changes. 我只想在js文件中的某些数据发生变化时刷新页面。 Appreciate your help if you can explain me with bit of code. 如果您能用一些代码解释我,请感谢您的帮助。 I searched all over internet, I couldnt find appropriate answer. 我在互联网上搜索,我找不到合适的答案。

Let's break this down. 让我们打破这个。 You want to: 你想要:

  • check something periodically 定期检查一下
  • the thing you want to check is a remote file 你要检查的东西是一个远程文件
  • IF it has changed you want to take an action 如果它已经改变,你想采取行动

As mentioned in the comments, there are ways to do all of the above in Javascript. 正如评论中所提到的,有很多方法可以在Javascript中完成上述所有操作。

First let's address checking something periodically: you can either use a recursive setTimeout loop, or setInterval : 首先让我们定期检查一些东西:你可以使用递归的setTimeout循环,也可以使用setInterval

function waitOneSecond = function() {
    setTimeout(function() {
        check(); // will get to this in a second
        waitOneSecond();
    }, 1000); // 1000 = one second
});

function checkEverySecond() {
    setInterval(function() {
        check();
    }, 1000); // 1000 = one second

Next, we need a way to check whether the file changed. 接下来,我们需要一种方法来检查文件是否发生了变化。 The actual "did it change part" is up to you (you might want to consider using a function like Underscore or Lodash's _.isEqual for it), but to do that you'll need to get the latest version of the JSON file. 实际的“它改变了部分”取决于你(你可能想考虑使用像Underscore或Lodash的_.isEqual这样的函数),但要做到这一点,你需要获得最新版本的JSON文件。 Luckily jQuery has a method for doing exactly that: getJSON : 幸运的是jQuery有一个方法可以做到这一点: getJSON

function check() {
    $.getJson('http://yourserver.com/data.js').done(function(json) {
        if (didChange(json)) {// use _.isEqual to write your didChange
            takeAction();
        }
    });
}

As you can see this last example also covers taking action, but again I'll leave the definition of the takeAction function to you. 正如您所看到的,最后一个示例也涵盖了采取行动,但我将再次将takeAction函数的定义takeAction您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我的json数据文件发生更改时如何刷新页面 - How to refresh a page whenever my json data file changes ReactApp-当我在VS代码编辑器中将js文件中的更改保存时,浏览器不会刷新页面。 - ReactApp - The browser does not refresh the page when i save changes in my js file in VS code editor . 如何在lobb.js中实现页面刷新(从服务器获取数据并丢失所做的所有更改)功能? - How to implement page refresh (fetch data from server and losing all changes made) functionality in backbone.js? 我如何使next.js表单输入字段中的数据在刷新页面后保留? - How do i make the data in the input feild of my form in next js stay after refresh of the page? 如何检查Json文件更新以及是否更新,然后使用更新的数据刷新我的页面 - how to check Json file update and if updated then refresh my page with updated data 每当我刷新页面时如何显示文件夹中的随机图像? - How to display random image from folder whenever I refresh the page? 在MySQL数据库中的数据更改后刷新页面 - Refresh a page after data in MySQL database changes 用户离开页面时如何删除文件? - How to delete a file whenever user leaves page? Vue JS - 如何在 window 大小发生变化时获取它 - Vue JS - How to get window size whenever it changes Angular Js:如何避免页面刷新时$ http调用的数据丢失? - Angular Js: How to avoid loss of data of $http call on page refresh?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM