javascript - tell jquery mobile / phonegap app to wait until content is loaded -
i've got phonegap app using jquery, jquery mobile, js, css & html.
i use listview show , filter lot of li's. when lists text work great. when (as want) li's solely populated images - random number of them fail load, leaving blank li's.
(when testing on pc in chrome works fine. fails once ut's phonegap'd , running on phone app).
if slow index page adding few hundred lines of useless code - images display.
so think problem page loads , finalises before images have loaded.
is there way tell app not load until images ready?
edit: code i'm using:
<div data-role="content"> <ul data-role="listview" data-icon="false" data-filter="true" data-filter-placeholder="search cards..." data-inset="true"> <li data-role="list-divider">list of things</li> <li data-filtertext="various:different:keywords"><div><img src="img/01046.jpg" class="cardimg"></div></li> <li data-filtertext="various:different:keywords"><div><img src="img/01047.jpg" class="cardimg"></div></li> <li data-filtertext="various:different:keywords"><div><img src="img/01048.jpg" class="cardimg"></div></li> ...
and on. there lot of images, , number going bigger on time.
you should add <li>
containing image <ul>
when each of image loaded. inside image callback img.onload
add <li>
, refresh jqm filterable listview.
here example:
var imageslist = [ {searchwords: "lewis hamilton", src: "https://via.placeholder.com/2323x2323"}, {searchwords: "sebastian vettel", src: "https://via.placeholder.com/1212x1212"}, {searchwords: "kimi raikkonen", src: "https://via.placeholder.com/2333x2333"}, {searchwords: "felipe massa", src: "https://via.placeholder.com/2000x2000"}, {searchwords: "valtteri bottas", src: "https://via.placeholder.com/1111x1111"}, {searchwords: "daniel ricciardo", src: "https://via.placeholder.com/666x666"}, {searchwords: "nicolas hulkenberg", src: "https://via.placeholder.com/2222x2222"} ], remaining; function addtolist() { remaining--; var head = $("#page-images-list .ui-header h1"); var msg = (remaining === 0) ? "all images loaded." : "remaining: " + remaining; $(head).html(msg); var html = '<li data-filtertext="'+this.searchwords+'">'; html += '<a href="#">'; html += '<img src="'+this.src+'">'; html += '<h2>'+this.searchwords+'</h2>'; html += '<p>'+this.src+'</p></a>'; html += '</li>'; $("#listview-images").append(html); //$("#listview-images").listview("refresh"); $("#listview-images").filterable("refresh"); } function loadimages(dest, list) { $(dest).empty(); remaining = list.length; $.each(list, function(index, item) { var img = new image(); img.searchwords = item.searchwords; img.onload = addtolist; settimeout(function() { img.src = item.src; }, 0); }); } $(document).on("vclick", "#btn-reload", function(e) { loadimages("#listview-images", imageslist); }); $(document).on("pagecontainershow", function(e, ui) { if (typeof ui.topage == "object") { switch (ui.topage.prop("id")) { case "page-images-list": loadimages("#listview-images", imageslist); break; } } });
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css"> <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script> </head> <body> <div id="page-images-list" data-role="page"> <div data-role="header" data-position="fixed"> <a href="#" id="btn-reload" class="ui-btn-left ui-btn ui-btn-inline ui-mini ui-corner-all ui-btn-icon-left ui-icon-refresh">reload</a> <h1>images callback</h1> </div> <div role="main" class="ui-content"> <ul data-role="listview" data-inset="true" data-filter="true" id="listview-images"> </ul> </div> </div> </body> </html>
additional note:
if using list divider
group list content, please note image arrival sequence not guaranteed in same order has been defined in images list array. depends how big each image , network traffic. so, if additionally need group list items in way, need additional code sort again list elements when each images loaded. if images have been downloaded , cached browser, array order (probably) maintained.
Comments
Post a Comment