First: Sorry for my Bad English. I hope you understand me anyway.
So. I have an Ajax request and the JSON file is always rewritten from somewhere else. The new data arrives but at the first change it takes 20-30 seconds until the change is visible (I think this is because of the caching, more about this below). After that, after the first request, it takes only 2 seconds or often shorter. And if I don’t change anything for a while the same process begins again.
I think it is either because of the caching or because I use a local HTTP server for the request. And this server I set up with Python, here the code from the server:
class ServerHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
http.server.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
logging.error(self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
for item in form.list:
logging.error(item)
http.server.SimpleHTTPRequestHandler.do_GET(self)
with open("data.txt", "w") as file:
for key in form.keys():
file.write(str(form.getvalue(str(key))) + ",")
def start_localhost():
Handler = ServerHandler
httpd = socketserver.TCPServer(("", 2029), Handler)
print("serving at port", 2029)
httpd.serve_forever()
And that it is the caching would also be funny, because I have the following in my head tag of the HTML file:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
What might be worth mentioning, I have this html file open in an Edge WebView window.
And here is my JavaScript Code:
var cur_width;
var cur_height;
var cur_pos;
var cur_right;
var cur_bottom;
var cur_brightness;
var cur_blur;
var cur_zoom;
var cur_contrast;
var cur_invert;
var cur_sepia;
var cur_saturate;
var cur_grayscale;
function checker() {
vid = document.getElementById("bg_div");
$.ajax({
type: "GET",
url: 'http://localhost:2029/backgrounds/asus/video_settings.json',
contentType: "application/json",
dataType: "json",
success: function(json_infos) {
var width = json_infos.width;
var height = json_infos.height;
var pos = json_infos.position;
var right = json_infos.right;
var bottom = json_infos.bottom;
var blur = json_infos.blur;
var brightness = json_infos.brightness;
var zoom = json_infos.zoom;
var contrast = json_infos.contrast;
var invert = json_infos.invert;
var sepia = json_infos.sepia;
var saturate = json_infos.saturate;
var grayscale = json_infos.grayscale;
var new_filter = "brightness("+brightness+") blur("+blur+") contrast("+contrast+") invert("+invert+") sepia("+sepia+") saturate("+saturate+") grayscale("+grayscale+")";
if(width != cur_width) {
cur_width = width;
vid.style.width = width;
}
if(cur_pos != pos) {
cur_pos = pos;
vid.style.position = pos;
}
if(cur_height != height) {
cur_height = height;
vid.style.height = height;
}
if(cur_right != right) {
cur_right = right;
vid.style.right = right;
}
if(cur_bottom != bottom) {
cur_bottom = bottom;
vid.style.bottom = bottom;
}
if(cur_brightness != brightness) {
cur_brightness = brightness;
vid.style.filter = new_filter;
}
if(cur_blur != blur) {
cur_blur = blur;
vid.style.filter = new_filter;
}
if(cur_zoom != zoom) {
cur_zoom = zoom;
vid.style.transform = zoom;
}
if(cur_contrast != contrast) {
cur_contrast = contrast;
vid.style.filter = new_filter;
}
if(cur_invert != invert) {
cur_invert = invert;
vid.style.filter = new_filter;
}
if(cur_sepia != sepia) {
cur_sepia = sepia;
vid.style.filter = new_filter;
}
if(cur_saturate != saturate) {
cur_saturate = saturate;
vid.style.filter = new_filter;
}
if(cur_grayscale != grayscale) {
cur_grayscale = grayscale;
vid.style.filter = new_filter;
}
}
});
}
checker();
var t = setInterval(checker, 400);
