Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.80 KB | None | 0 0
  1. var VerificaChat = function(TempoRequest){
  2.                         Interval = setInterval( function(){
  3.                         $.ajax({
  4.                             url: 'chat.php',
  5.                             dataType: 'JSON',
  6.                             type: 'POST',
  7.                             data: 'type=recebeMsg&lobby=' + $('.lobbyid').val(),
  8.                             success: function(data){
  9.                                 console.log(data);
  10.                                 if(data.status === 'success'){
  11.                                    
  12.                                         for( var i = 0; i < data.msgs; i++){
  13.            
  14.                                             $('#chat_box').append('<div class="msg user_' + data[i]['time'] + '"><span>'+ data[i]['nick'] + ': </span>' + data[i]['contente'] +'</div)');
  15.                                         }
  16.  
  17.                                         $('#chat_box').scrollTop($('#chat_box').prop("scrollHeight"));
  18.  
  19.                                 }  
  20.                                 if(data.status === 'notnew'){
  21.                                    
  22.                                     RequestsNotNew++;
  23.                                     if(RequestsNotNew > 5){
  24.                                         TempoRequest = 5000;
  25.                                         clearInterval(Interval);
  26.                                         VerificaChat(TempoRequest);
  27.                                     }
  28.                                     if(RequestsNotNew > 10){
  29.                                         TempoRequest = 10000;
  30.                                         clearInterval(Interval);
  31.                                         VerificaChat(TempoRequest);
  32.                                     }
  33.                                     if(RequestsNotNew > 20){
  34.                                         TempoRequest = 25000;
  35.                                         clearInterval(Interval);
  36.                                         VerificaChat(TempoRequest);
  37.                                     }
  38.                                 }
  39.                                 console.log(RequestsNotNew);
  40.                             },
  41.                             error: function(data){
  42.                                 TempoRequest = 18000;
  43.                                 console.log(data);
  44.                                 clearInterval(VerificaChat);
  45.                                 VerificaChat(TempoRequest);
  46.                                 $('#chat_box').append('<div class="msg msg_erro">Ocorreu um problema de conexão ao chat. Você pode não ver as novas mensagens.</div)');
  47.                                 $('#chat_box').scrollTop($('#chat_box').prop("scrollHeight"));
  48.                             }
  49.                         });
  50.  
  51.                     }, TempoRequest );
  52.                 }
  53.  
  54.                 var TempoRequest = 3000;
  55.                 var Ativo = true;
  56.                 var RequestsNotNew = 0;
  57.                 if(Ativo === true){
  58.                     VerificaChat(TempoRequest);
  59.                 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement