简单 Ajax 加载页面的计时器


Timer for Simple Ajax loaded page

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

提前感谢您的时间

.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!';
?>

你想要 setTimeout Javascript 函数。

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

这将等待 5 秒才能设置内容。

您可以使用

setTimeout:

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

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