Reload Page with Javascript after Database changes

荒凉一梦 提交于 2020-01-03 03:00:19

问题


I'm displaying a Table from a Database like so

<tr>
  <td>{{ $user->id }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td>{{ $user->... }}</td>
  <td></td>
</tr>

If something changes in the Database I have to hit F5 in the Browser to Display the changes. Now I want to refresh the Page with Javascript, each time something changes in the Database. I don't want to refresh the page on a time intervall.
I have an Admin who can change someones Status and you can change your status yourself. When someone changes his own status the Admin Backend should refresh.

Do you guys have any ideas on that?


回答1:


The easiest way (which has already been mentioned) would be to reload the page every x seconds.

This means however, that if the user is browsing / editing anything on the page, he only has x seconds to finish this before all the progress is lost.

You could use AJAX to check the database for changes however:

  • Make a DB-Check.php script that outputs the latest database entry
  • In your main file, include an AJAX script which pulls the DB-Check.php every 30 seconds
  • Put the output from the DB-check.php in a new JS variable
  • If the new variable is different from the old variable, refresh the whole page (Or use AJAX to load the rest of the database)



回答2:


The easiest solution is to refreshing the page automatically in second. So, please add this inside the head:

<meta http-equiv="refresh" content="30" />

to refresh it every 30 seconds. Others ways are by javascript:

setTimeout('window.location.reload();', 30000);

or

setTimeout('history.go(0);', 30000);

or

setTimeout("location.reload(true);", timeoutPeriod);



回答3:


location.reload();

this is the javascript function to reload the current page. You can use this function inside the js function when do you want this.

this Question is a bit very large, so if you want the correct answer according to your scenarios please describe your detail scenarios.

you may be looking for like this How to automatically reload a web page at a certain time?




回答4:


I would do this using very primitive API. jQuery (or JS) makes request to API url which returns boolean, and using this boolean, true would trigger refresh page. (or even better- change content without with jQuery without refreshing) that would be my approach.



来源:https://stackoverflow.com/questions/25720086/reload-page-with-javascript-after-database-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!