问题描述
我目前使用在我的网页此code:
I'm currently using this code on my webpage:
<?php
$url = "https://www.toontownrewritten.com/api/invasions";
$data = json_decode(file_get_contents($url));
if (!empty($data->invasions)) {
echo "<h1 style='text-align:center;margin:auto;padding:2px;font-size:16px;font-weight:bold;text-decoration:underline;padding:2px;'>Invasion Tracker</h1>";
$i = 0;
foreach($data->invasions as $title => $inv) {
print "<h3 style='text-align:center;margin:auto;'><b>District:</b> {$title}
</h3><br style='font-size:1px;'><h3 style='text-align:center;margin:auto;'><b>Cog:</b> {$inv->type}
</h3><br style='font-size:1px;'><h3 style='text-align:center;margin:auto;'><b>Progress:</b> {$inv->progress}
</h3>";
if (count(($data->invasions) > 1)) {
if (end($data->invasions) !== $inv) {
print "<hr>";
} else {
print "<br style='font-size:2px;'>";
}
}
}
} else {
echo "<h1 style='text-align:center;margin:auto;padding:2px;color:darkred;font-weight:bold;'>No invasions!</span>";
}
?>
我在寻找,使其通过AJAX刷新每10秒。不过,我一直在读书,你需要做一个函数,但我不知道我怎么会做的API?每隔10秒,该API被更新,这就是为什么我想这与AJAX可以每10秒更新。目前,我有这样的用户必须手动刷新。任何帮助AP preciated!
I'm looking to make it refresh every 10 seconds via AJAX. However, I keep reading you need to make a function, but I'm not sure how I'd do that with the API? Every 10 seconds, that API is being updated, which is why I'd like this to be updated with AJAX every 10 seconds. Currently, I have it so the user has to manually refresh. Any help is appreciated!
推荐答案
您可以简单地重新加载所提出的方法here
但是,如果你想有一个AJAX的实现,它只是refereshes你的HTML漂亮整洁的一部分,你不得不
But if you wanna have an AJAX implementation which just refereshes a part of your html nice and tidy, You gonna have to
- 差点忘记你的PHP code
-
使用下面的code来实现请求的url
- Almost forget your PHP code
use the following code to implement the request to the url
$。阿贾克斯({ 网址:https://www.toontownrewritten.com/api/invasions}) .done(功能(数据){ 如果(控制台和放大器;&放大器;的console.log){ 的console.log(数据); } });
$.ajax({ url: "https://www.toontownrewritten.com/api/invasions",}) .done(function( data ) { if ( console && console.log ) { console.log( data ); } });
请在 JS
code这将转换数据
获得在previous第一个可读的HTML,并显示在页面上。它应该在的块来实现,其中的console.log(数据)
是
Make a JS
code which would convert the data
got in the previous section to a readable html and show it on your page. It should be implemented in the the block where console.log(data)
is.
把code这部分在的setInterval
Put that part of code in a setInterval
的setInterval(函数(){ // $阿贾克斯();},10000);
setInterval(function(){ //$.ajax();}, 10000);
和注意,你会下地狱,如果你的要求doen't完整的时间间隔。看到这。
And be aware that you are gonna go to hell if your request doen't complete in the interval. see this .
这篇关于使用AJAX自动更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!