개발 Q&A

제목 ajax 통해 게시판 데이터 불러오기 전까지 로딩 이미지 첨부 기능 구현할려고합니다.
글쓴이 amkorjquery 작성시각 2016/09/09 10:58:15
댓글 : 1 추천 : 0 스크랩 : 1 조회수 : 13107   RSS

 

<td>
 <label>이름 <input type="text" id="sh_u_id" name="sh_u_id" value="<?=$sh_user_id?>"></label>
 <button type="button" onclick="load_list('loading');">조회</button>  // 여기가 start 입니다!!
</td>
 
		<table cellspacing="0"width="100%" style="margin-left: 10px; width: 100%;">
			<thead>
				<th>번호</th>
				<th>이름</th>
				<th>제목</th>
			</thead>
			<tbody id="list_datas"></tbody>
 
</talbe>
<div id="loading_html"></div> 
<script>
$(document).ready(function(){
  var loading = $('<div id="loading" class="loading" style="display:none"><img id="loading_img" alt="loading" src="./img/loading.gif"/ > </div>'); 
  $('#loading_html').append(loading);  // 로딩 이미지 append 하기
 
});
 
 
function load_list(loading){
if(loading) $('#loading').show(); // 검색 통해 ajax 실행되는거면 로딩 이미지 show하기
var sh_u_id = $('#sh_u_id').val();
 $.ajax({
            url: 링크주소.
             data: {"sh_u_id":sh_u_id },
            async: false,
            dataType: "jsonp",
            jsonp: "callback",
            type:"get",
            success: function (response) {
                outputdebugstring(response);
                if (is_success(response)) {  //성공시!!!!!!!
                   
for(var i=0; i<response.lenght; i++){
  
   var html = '<tr>';
html += '<td>'+번호+'</td>';
html += '<td>'+이름+'</td>'; 
html += '<td>'+제목+'</td>';
}
$(#list_datas).append(html); // 게시판 데이터 append 하기
//$('#loading').hide(); // 게시판 데이터 hide 처리 
 
 
                } else {
                    outputdebugstring(response);
                }
            },
            error: function (response) {
                outputdebugstring("load_user_info 호출이 실패 하였습니다.");
                outputdebugstring(response);
            },
            timeout: 30000
 
        });
 
}
</script>

 

 

 

해당 검색해야 되는 이름 적고 "조회"버튼 눌으면 자바스크립트 load_list 함수로  호출하게되는데

 

여기서 로딩 이미지는 나타나지만 ajax 성공 후 이미지가 이제서야 나옵니다.

 

순서상으로는 ajax 보다 if(loading) $('#loading').show();

 

앞에 있기때문에 이렇게 소스 작성했는데요.

 

왜 반응이 늦게 나오나요??

 다음글 ajax 을 통해 출력한 input 필드에 datapi... (11)
 이전글 cli 실행 오류. (7)

댓글

한대승(불의회상) / 2016/09/09 11:32:03 / 추천 0

순서가 바껴서 그러네요.

ajax 들어가기전에 loading 이미지를 보여주고 ajax 통신하고 success가 떨어지면 loading 이미지를 감추면됩니다.