【问题标题】:Timer for Simple Ajax loaded page简单 Ajax 加载页面的计时器
【发布时间】:2014-12-09 20:37:17
【问题描述】:

有一个simple ajax 用于将内容加载到页面中。 我想要的是加载的内容在页面上显示之前等待一段时间。 (在淡入之前) 在 #body 淡入之后,我尝试使用 jQuery 的 .delay() 函数,但它不起作用。 谁能指出我正确的方向?

提前感谢您的宝贵时间

JS

var default_content="";

$(document).ready(function(){

checkURL();
$('ul li a').click(function (e){

        checkURL(this.hash);

});

//filling in the default content
default_content = $('#pageContent').html();


setInterval("checkURL()",250);

});

var lasturl="";

function checkURL(hash)
{
if(!hash) hash=window.location.hash;

if(hash != lasturl)
{
    lasturl=hash;

    // FIX - if we've used the history buttons to return to the homepage,
    // fill the pageContent with the default_content

    if(hash=="")
    $('#pageContent').html(default_content);

    else
    loadPage(hash);
}
}


function loadPage(url)
{
url=url.replace('#page','');
$('#body').hide ();
$('#loading').css('visibility','visible');


$.ajax({
    type: "POST",
    url: "load_page.php",
    data: 'page='+url,
    dataType: "html",
    success: function(msg){

        if(parseInt(msg)!=0)
        {
            $('#pageContent').html(msg);
            $('#body').fadeIn( 'slow' );
            $('#loading').css('visibility','hidden');
        }
    }

});

}

PHP

if(!$_POST['page']) die("0");

$page = (int)$_POST['page'];

if(file_exists('pages/page_'.$page.'.html'))
echo file_get_contents('pages/page_'.$page.'.html');

else echo 'There is no such page!';
?>

【问题讨论】:

    标签: javascript php jquery html ajax


    【解决方案1】:

    你想要 setTimeout Javascript 函数。

    setTimeout(function(){
        $('#pageContent').html(msg);
        $('#body').fadeIn( 'slow' );
        $('#loading').css('visibility','hidden');
    }, 5000);
    

    这将等待 5 秒来设置内容。

    【讨论】:

      【解决方案2】:

      你可以使用 setTimeout:

      setTimeout(checkURL(/* value to pass */),250);
      

      请不要引用函数调用,因为 " " 中的内容将作为字符串处理,而不是函数调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-28
        • 2012-03-01
        • 2016-11-06
        • 2013-07-30
        • 1970-01-01
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多