So on start a loader should be there and as soon as weather is shown the preloader
should be gone.It's using simpleweather.js
plugin.
I've tried using .hide()
.addClass()
.Doesn't seems to work.
//JS
$(document).ready(function() {
navigator.geolocation.getCurrentPosition(function(position) {
loadWeather(position.coords.latitude+','+position.coords.longitude);
});
function loadWeather(location, woeid) {
$.simpleWeather({
location: location,
woeid: woeid,
unit: 'c',
success: function(weather) {
html = '<h1><i class="wi wi-fw wi-yahoo-'+weather.code+'"></i></h1>';
html += '<h2>'+weather.temp+'°'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li class="currently">'+weather.currently+'</li></ul>';
$("#weather").hide().html(html).fadeIn("slow");
},
error: function(error) {
$("#weather").html('<p>'+error+'</p>');
}
});
}
});
//HTML
<body>
<div id="weather"></div>
</body>
Only as idea:
create a loading animation in your html, it should be visible by default, so user see it directly on page visit. Then hide it on "success" in your js.
<div id="loading">Loading....</div>
and js code
jQuery("#loading").hide()
To be able to show a loader, do the following. Add a div with img tag which points to the loader file.
<div id="loader" style="display:none"><img src="<path_to_loader_file>"></div>
Update your function:
function loadWeather(location, woeid) {
var load = $('#loader');
load.show();
$.simpleWeather({
location: location,
woeid: woeid,
unit: 'c',
success: function(weather) {
load.hide();
},
error: function(error) {
load.hide();
}
});
}
You can use ajaxSetup
$.ajaxSetup({
global: true,
beforeSend: function(xhr, settings) {
$('#some-overlay-with-spinner').show();
},
complete: function(xhr, status) {
$('#some-overlay-with-spinner').hide();
},
error: function(xhr, status, error) {
$('#some-overlay-with-spinner').hide();
},
timeout: 5000
});
$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
$('#some-overlay-with-spinner').hide();
});
UPDATE: Try topbar as progress indicator. You can show and hide it instead of an overlay with a spinner.
add this code
<div id="loading">Loading....</div> // use a gif image loader within this div
$("#loading").show(); //before ajax request
your ajax code --here
$("#loading").hide(); // after ajax response
$(document).ready(function() { loadWeather('51.5034070,-0.1275920'); }); function loadWeather(location, woeid) { $("#weather").html('<img src="http://www.arabianbusiness.com/skins/ab.main/gfx/loading_spinner.gif">'); $.simpleWeather({ location: location, woeid: woeid, unit: 'c', success: function(weather) { html = '<h1><i class="wi wi-fw wi-yahoo-' + weather.code + '"></i></h1>'; html += '<h2>' + weather.temp + '°' + weather.units.temp + '</h2>'; html += '<ul><li>' + weather.city + ', ' + weather.region + '</li>'; html += '<li class="currently">' + weather.currently + '</li></ul>'; $("#weather").hide().html(html).fadeIn("slow"); }, error: function(error) { $("#weather").html('<p>' + error + '</p>'); } }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script> <body> <div id="weather"></div> </body>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.