javascript - ajax非同步請求數據,ios螢幕閃爍

WBOY
發布: 2023-03-02 13:36:01
原創
1168 人瀏覽過

做一個聊天頁面,ajax異步請求數據時,不論有無數據返回,ios都會閃動一下,而android不會閃動 ,這是怎麼回事,該如何解決

<code>$(document).ready(function(){
        $('.loading_box').attr('style','display:none');
        ajaxform();
        window.setInterval("ajaxform()",3000); 

    });
    

function ajaxform(){
    var id=$(".main_box ul li:last-child").find('.delete_btn').attr('field');//alert(id);
    $.post(
        "{:U('App/Friends/ajaxform')}",
        {"openid":openid,"id":id},
        function(data){
          if (data) {
            var str = "";console.log(data);
            $.each(data, function(index, array) {
              if(array['openid']==openid){
                if(array['type']==0){
                  //普通文字或图片
                  str='<li class="chart-right"><div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_right"><p>'+array['content']+'</p></div><div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div></div></div><div class="persion_img_right"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div></li>';
                }else{
                  //我的霸屏
                  str='<li class="chart-right"><div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_right"><p><i class="money_text">重金霸屏'+arr[array['type']]+'秒</i>'+array['content'];
                  if(array['img']!=""){
                    str+='<font class="chart_img"><img src="'+array['img']+'"></font>';
                  }
                  str+='</p></div><div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div></div></div> <div class="persion_img_right"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div> </li>';
                }  
              }else{
                if(array['type']==0){
                  //普通文字或图片
                  str = ' <li class="chart-left"><div class="persion_img_left"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div> <div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_left"><span class="sanjiao_left"></span><p>'+array["content"]+'</p></div>';
                  if(isadmin){
                    str+='<div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div> <div class="blacklist" field="'+array['openid']+'">拉黑</div></div>';
                  }
                  str+='</div> </li>';
                }else{
                  //他人霸屏
                  str='<li class="chart-left "><div class="persion_img_left"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div> <div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_left"><span class="sanjiao_left"></span><p><i class="money_text">重金霸屏'+arr[array['type']]+'秒</i>'+array['content'];
                  if(array['img']!=""){
                    str+='<font class="chart_img"><img src="'+array['img']+'"></font>';
                  }
                  str+='</p></div>';
                  if(isadmin){
                    str+='<div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div> <div class="blacklist" field="'+array['openid']+'">拉黑</div></div>';
                  }
                  str+='</div> </li>';
                }
              }
              $(".main_box ul").append(str);
              $('.main_box').scrollTop( $('.main_box')[0].scrollHeight );
            });
          }
                     
        }
    );
}</code>
登入後複製
登入後複製

回覆內容:

做一個聊天頁面,ajax異步請求數據時,不論有無數據返回,ios都會閃動一下,而android不會閃動 ,這是怎麼回事,該如何解決

<code>$(document).ready(function(){
        $('.loading_box').attr('style','display:none');
        ajaxform();
        window.setInterval("ajaxform()",3000); 

    });
    

function ajaxform(){
    var id=$(".main_box ul li:last-child").find('.delete_btn').attr('field');//alert(id);
    $.post(
        "{:U('App/Friends/ajaxform')}",
        {"openid":openid,"id":id},
        function(data){
          if (data) {
            var str = "";console.log(data);
            $.each(data, function(index, array) {
              if(array['openid']==openid){
                if(array['type']==0){
                  //普通文字或图片
                  str='<li class="chart-right"><div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_right"><p>'+array['content']+'</p></div><div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div></div></div><div class="persion_img_right"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div></li>';
                }else{
                  //我的霸屏
                  str='<li class="chart-right"><div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_right"><p><i class="money_text">重金霸屏'+arr[array['type']]+'秒</i>'+array['content'];
                  if(array['img']!=""){
                    str+='<font class="chart_img"><img src="'+array['img']+'"></font>';
                  }
                  str+='</p></div><div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div></div></div> <div class="persion_img_right"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div> </li>';
                }  
              }else{
                if(array['type']==0){
                  //普通文字或图片
                  str = ' <li class="chart-left"><div class="persion_img_left"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div> <div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_left"><span class="sanjiao_left"></span><p>'+array["content"]+'</p></div>';
                  if(isadmin){
                    str+='<div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div> <div class="blacklist" field="'+array['openid']+'">拉黑</div></div>';
                  }
                  str+='</div> </li>';
                }else{
                  //他人霸屏
                  str='<li class="chart-left "><div class="persion_img_left"><img src="'+array['headimgurl']+'"><font><img src="{$tpl}App/Friends/Public/img/vipv'+array['classid']+'l.png"></font></div> <div class="chart_text_box"><div class="jieshao"><p>'+array['uname']+'</p><p>'+array['time']+'</p></div><div class="chart_text_qipao_left"><span class="sanjiao_left"></span><p><i class="money_text">重金霸屏'+arr[array['type']]+'秒</i>'+array['content'];
                  if(array['img']!=""){
                    str+='<font class="chart_img"><img src="'+array['img']+'"></font>';
                  }
                  str+='</p></div>';
                  if(isadmin){
                    str+='<div class="operation_box"><div class="delete_btn" field="'+array['id']+'">删除</div> <div class="blacklist" field="'+array['openid']+'">拉黑</div></div>';
                  }
                  str+='</div> </li>';
                }
              }
              $(".main_box ul").append(str);
              $('.main_box').scrollTop( $('.main_box')[0].scrollHeight );
            });
          }
                     
        }
    );
}</code>
登入後複製
登入後複製

閃動是頁面刷新了,看是否有重新渲染頁面的程式碼。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!