2016-03-26 49 views
0

我正在使用codeigniter,我的控制器中有一些cURL腳本,它會得到一些大的json數據並加載大約15-20秒。加載應用程序後使用'加載'捲曲數據

我的看法是加載時間很長,像捲曲時間(15-20s)。

如何首先加載我的視圖與一些「加載圖標」,並在腳本後將下載所有JSON數據替換「加載圖標」與我的數據?

我的代碼:

查看

 <?php foreach ($items as $item): ?> 
      <div class="item-slot"> 
       <img src="http://cdn.steamcommunity.com/economy/image/<?= $item['icon_url'] ?>"> 
       <p><?= $item['name'] ?></p> 
      </div> 
     <?php endforeach; ?> 

控制器(open_inventory功能)

public function open_inventory($steam_id, $appid = 730, $contextid = 2) 
{ 
    $data = array("steamid" => $steam_id, "appid" => $appid);                  
    $data_string = json_encode($data);                     

    $ch = curl_init('http://localhost:3000/inventory');                  
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
     'Content-Type: application/json', 
     'Content-Length: ' . strlen($data_string)) 
    ); 
    $json = json_decode(curl_exec($ch),true); 

    if($json['data']) 
    { 
     return $json['data']; 
    } 
    return false; 
} 

$items中視圖是從控制器$json['data']

問候。

+1

你需要考慮使用'ajax'電話等應用檢索異步數據從服務器。 – Yani

+0

@Yani你能告訴我,我需要搜索ajax函數來做到這一點?問候。 – mateuszji

+0

http://api.jquery.com/jquery.ajax/ – Yani

回答

0

jQuery有功能ajaxStart和ajaxStop,你可以使用這樣的:

$(document).ajaxStart(function() { 
    $("#loading-image").show(); 
}).ajaxStop(function() { 
    $("#loading-image").hide(); 
}); 

更多信息herehere

+0

你能解釋我更多如何使用php加載curl json數據嗎? – mateuszji

+0

我也喜歡,但你需要更新你的問題,並顯示你現在正在做什麼。請只顯示重要的代碼! – Vickel

+0

我編輯了我的問題。 – mateuszji