I tried to create a small project to track flying over my area.
Using python package for flight-radar24 API, I successfully got the needed data.
I’ve created systemd service to launch my python script every x seconds. This script output a html file named "data.html".
Each plane output two div: One visible with few information, one invisible with more detailed information.
Second part of the project is to show the retrieved data. I proceed as follow:
- 1/ simple webpage (index.html)
- 2/ CSS file
- 3/ A clock (JavaScript, just to show the time) on the "header" part of index.html
- 4/ jQuery (to use jQuery, locally downloaded)
- 5/ A reloading JavaScript (fetch and print data.html into the "datadiv" of index.html)
- 6/ A "popup" manager, so you can see a nice div with additional information while clicking on a plane.
My problem is that I failed at stopping the auto-reloading mechanism when the "popup" is visible. If you click on a plane while the reloading is scheduled in 1 or 2 seconds, you don’t have time to read.
I tried using "clearInterval/clearTimeout" in the click function without any success.
I tried the following:
- Merging some js file, thinking I could have a variable scope issue.
- Creating the "popup" div in index.html and keeping it empty, then just change the content using jQuery so this div exists before the data.html is loaded.
- Using setTimeout instead of setInterval.
- And to many thing I can’t remember…
The popup code (each plane ("trigger") got it’s own div ID with additional information (eXYZ)):
$(function() {
$('.trigger').click(function(e) {
$('.plane_info').hide();
$('div#e'+this.id).show();
});
$('.trigger').mouseleave(function(e) {
$('div#e'+this.id).hide();
});
});
The reloading code:
function setupRefresh()
{
setInterval("refreshBlock();",5000);
}
function refreshBlock()
{
$('#datadiv').load("../data.html");
}
setupRefresh();
3
Answers
Ah, I see you’re facing an interesting challenge with your project! To address the issue of preventing automatic reloading when the "popup" is visible, you can modify the reloading code by adding a condition to check if the popup is currently visible before triggering the reload. Let’s make the necessary adjustments:
You omitted the part of the code where you tried using
clearInterval
… but given that you aren’t capturing the return value ofsetInterval
, I can guess what went wrong:In order to use
clearInterval
you need to capture the interval ID when setting the interval, so the browser can know which one to clear.Let’s try a simple approach:
isVisible
that tracks whether the popup is visible or notrefreshBlock
if the popup is not visible