I have looked at several similar questions before, and none of them match the situation I'm having. I have two div blocks that look like this:
#container {
width: 800px;
height: 600px;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
color: #fff;
}
.status {
height: 100px;
width: 100px;
background: #efefef;
margin-left: 2px;
border: 1px solid #ccc;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
border-radius: 3px;
}
.online {
background: #8BC34A;
}
.offline {
background: #e53935;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">'
<div id="loading"><img src="http://i.stack.imgur.com/KZ3Hi.gif" style="display: none;" /></div>
<div id="user1" class="status"><span>user1</span></div>
<div id="user2" class="status"><span>user2</span></div>
</div>
result.php returns the status in the following format:
{ "user1": "online", "user2" : "offline" }
I want to display this gif on each div while the request completes. This is what I've tried:
$(document).ready(function(){
setInterval(function(){
$.get("result.php", function(data){
$('#loading').delay(3000).fadeOut();
var obj = jQuery.parseJSON(data);
user1_status = 'status ' + obj['user1'];
user2_status = 'status ' + obj['user2'];
$("#user1").attr("class", user1_status);
$("#user2").attr("class", user2_status);
});
}, 1000);
});
But this just shows the loading.gif on the very first load ALONG SIDE the divs. I want the gif to be shown as an overlay (or instead of the div), one for each div, while the request is being processed. I need the gif displayed only if the status is different from what it was before. That is, if the status changes from offline to online or vice versa, display the loading gif, else just show the div as it is.
What do I need to change?