简体   繁体   English

功能启动时,加载Spinner图像的速度过快

[英]Loading Spinner Image too fast at start of Function

I have a function that displays contents of a posts when clicked on. 我有一个功能,当单击时显示帖子的内容。 I want the loading spinner to display and delay for few sections before the post content appears. 我希望加载微调器在帖子内容出现之前显示并延迟几个部分。 The issue here is when I click on each post, the spinner appears for maybe 1ms and in some cases it disappears long before the content appears. 这里的问题是,当我单击每个帖子时,微调框可能会显示1毫秒,在某些情况下,它会在内容出现前很长时间消失。

function showPost(id) {
    setTimeout(function() {$('#loader').show();},1);
    $('#pcontent').empty();
    $.getJSON('http://howtodeployit.com/category/daily-devotion/?json=get_post&post_id=' + id + '&callback=?', function(data) {
    var $postcon = $('<div/>').append([$("<h3>", {html: data.post.title}),$("<p>", {html: data.post.content})]);
        $postcon.appendTo('#pcontent');
    });
}

Spinner HTML: 微调HTML:

<div id='loader'><img src="css/images/loader.gif"/></div> 

gif image always behave differently on every device..basically it depends upon device's processing speed. gif图像在每个设备上的行为总是不同的。基本上,它取决于设备的处理速度。 so better option is to use image sprites and animate it with javascript.. In your case at page load there is nothing processing..but as page starts to load device's processor cant handle the load and as a result your gif image gets slower 所以更好的选择是使用图像精灵并用javascript进行动画处理。.在页面加载时,没有任何处理..但是随着页面开始加载,设备的处理器无法处理加载,结果您的gif图像变慢了

It seems from your last commented line that you are using a timeout to hide the loader. 从您的最后一条评论行看来,您正在使用超时来隐藏加载程序。 Instead You should handle the hiding inside the callback function of your ajax request, so that loader hides after request is completed, not after a fixed amount of time: 相反,您应该处理ajax请求的回调函数内部的隐藏,以便加载程序在请求完成后隐藏,而不是在固定的时间后隐藏:

function showPost(id) {
    $('#loader').show();
    $('#pcontent').empty();
    $.getJSON('http://howtodeployit.com/category/daily-devotion/?json=get_post&post_id=' + id + '&callback=?', function(data) {
        $('#loader').hide();
        var $postcon = $('<div/>').append([$("<h3>", {html: data.post.title}),$("<p>", {html: data.post.content})]);
        $postcon.appendTo('#pcontent');
    });
}

Try this: 尝试这个:

function showPost(id) {

    $('#loader').show();
    $('#pcontent').empty();

    $.ajax({
        url: 'http://howtodeployit.com/category/daily-devotion/?json=get_post&post_id=' + id + '&callback=?',
        dataType: 'json',
        success: function (data) {
            var $postcon = $('<div/>').append([$("<h3>", {
                html: data.post.title
            }), $("<p>", {
                html: data.post.content
            })]);
            $postcon.appendTo('#pcontent');
            $('#loader').hide();
        }

    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM