2

从我的语法可以看出,我对 jQuery 很陌生。我收到一个错误

Uncaught SyntaxError: Unexpected identifier

我的代码:

jQuery(document).ready(function () {
    //trying to reload the content after 3 seconds.
    setTimeout( "jQuery('#div1').load("ajaxtest.php");",3000 );
});
4

3 回答 3

3

试一试,您将希望将 jquery 操作放在这样的函数中:

jQuery(document).ready(function () {
    //trying to reload the content after 3 seconds.
    setTimeout(function(){
        jQuery('#div1').load("ajaxtest.php");
    }, 3000 );
});
于 2013-05-29T21:11:40.450 回答
1

问题就在""里面""

这是使用转义字符更正的方法

jQuery(document).ready(function () {
   //trying to reload the content after 3 seconds.
   setTimeout( "jQuery('#div1').load(\"ajaxtest.php\");",3000 );
});

但是传入字符串setTimeout被认为是不好的做法。所以,你可以试试经典的外壳

jQuery(document).ready(function () {
    //trying to reload the content after 3 seconds.
    setTimeout((function(){jQuery('#div1').load("ajaxtest.php");}),3000 );
}); 

循环你而不是使用setInterval

jQuery(document).ready(function () {
    //trying to reload the content after 3 seconds.
    setInterval((function(){jQuery('#div1').load("ajaxtest.php");}),3000 );
}); 

并停止循环,您可以使用这样的变量

 var anything = 
     setInterval((function(){jQuery('#div1').load("ajaxtest.php");}),3000 );

并最终清除

 clearInterval(anything);
于 2013-05-29T21:15:00.677 回答
1

尝试:

jQuery(document).ready(function () {
    //trying to reload the content after 3 seconds.
    setTimeout( function() { $('#div1').load("ajaxtest.php")},3000 );
});

如果您需要它循环,这应该是合适的:

jQuery(document).ready(function () {
    //trying to reload the content after 3 seconds.
    setInterval( function() { $('#div1').load("ajaxtest.php"); },3000 );
});
于 2013-05-29T21:12:03.067 回答