在AJAX Success中超时HTML标记

时间:2015-10-27 04:36:30

标签: javascript ajax

我想在触发ajax Success时设置超时。所以在2秒后我们说,我需要成功标记消失。

这是我的代码,

$.ajax({
    url : 'process/register/registerDB.php',
    success : function (data, textStatus, jqXHR) {
      timer = setTimeout(successNotification, 2000);
    }
});

function successNotification {
  $('#alert-box')
    .html('<div class="alert alert-success" role="alert">User <b>'
          + newUserArr["fullname"]
          + '</b> Successfully Submitted</div>');
}

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

在Ajax中,你更喜欢setTimeout是一个不同的例子:

setTimeout(function () { successNotification(); }, 2000);

使用它可能会有所帮助

答案 1 :(得分:1)

define("MAX_EXECUTION_TIME", 10); # seconds

$timeline = time() + MAX_EXECUTION_TIME;

function check_timeout()
{
if( time() < $GLOBALS['timeline'] )
return;

# timeout reached:
echo "<html><html>Sorry, we are very busy, retry later!</body>        </html>";
exit;
}

register_tick_function("check_timeout");

declare( ticks=1 ){
# here the process that might require
# so much time; any output generated is
# keep inside a string $s
}

# Ok, process completed, output the result:
echo $s;

function myfunc() { $.ajax({ // your params here }).done(function (data) { // do something with the data }).always(function () { window.setTimeout(getChatMessages, 1101); }); } myfunc(); 的目的是在获取邮件时不会出现错误。

答案 2 :(得分:1)

请替换timer = setTimeout(successNotification,2000); to timer = setTimeout(&#34; successNotification&#34;,2000);

差异仅在引号中起作用,successNotification消息将在2秒后调用。