javascript - How to show loading gif while request is being processed? -


i have looked @ several similar questions before, , none of them match situation i'm having. have 2 div blocks 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 status in following format:

{ "user1": "online", "user2" : "offline" } 

i want display gif on each div while request completes. 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 shows loading.gif on first load along side divs. want gif shown overlay (or instead of div), 1 each div, while request being processed. need gif displayed if status different before. is, if status changes offline online or vice versa, display loading gif, else show div is.

what need change?

you can use jquery ajax call

// start loading gif $('#loading').start();

$.ajax({     type: "get",     url: 'result.php',     success: function(data)     {   // loading ends          $('#loading').fadeout();     },error: function (error)     {            // loading ends          $('#loading').fadeout();     }  });  

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -