Javascript中的For循环内部setTimeout不起作用
我正在尝试聊天应用程序,为此,我试图在Ajax调用中设置成功结果的延迟 我展示了一些东西,然后在一个循环中展示了一些建议 我只想在3秒钟后显示结果,因此我尝试了以下操作:Javascript中的For循环内部setTimeout不起作用,javascript,jquery,Javascript,Jquery,我正在尝试聊天应用程序,为此,我试图在Ajax调用中设置成功结果的延迟 我展示了一些东西,然后在一个循环中展示了一些建议 我只想在3秒钟后显示结果,因此我尝试了以下操作: $.ajax({ url: 'message.php', type: 'POST', data: 'text='+$value+'&count='+$count, success: function(result)
$.ajax({
url: 'message.php',
type: 'POST',
data: 'text='+$value+'&count='+$count,
success: function(result)
{
setTimeout(function()
{
suggestiondelay(result);
},3000);
}
});
function suggestiondelay(result)
{
result=result.split('&');
$replay = '<div class="bot-inbox inbox"><div class="icon"><img src="icon.png"></div><div class="msg-header"><p>'+ result[0] +'</p><div class="playicon" style="margin-top: 5px" id="'+ result[0] +'" onclick="voice_func(this.id)"><a href="#"><i class="fas fa-volume-up" aria-hidden="true"></i></a></div>Click to Listen</div></div>';
$("#suggestions").html("");
$(".form").append($replay);
var id="";
for(var i=1; i<result.length;i++)
{
id="bbc"+i;
$("#suggestions").append('<button class="bb" id='+id+' onclick="btn_click(this.id);">'+result[i]+'</button>' );
}
//when chat goes down, the scroll bar automatically comes to the bottom
$(".mainform").scrollTop($(".mainform")[0].scrollHeight);
}
$.ajax({
url:'message.php',
键入:“POST”,
数据:'text='+$value+'&count='+$count,
成功:功能(结果)
{
setTimeout(函数()
{
建议延迟(结果);
},3000);
}
});
功能建议延迟(结果)
{
结果=结果分割('&');
$replay=''+result[0]+'单击收听';
$(“#建议”).html(“”);
$(“.form”).append($replay);
var id=“”;
对于(var i=1;iI不理解…3秒后显示哪些其他内容?@devnull69-replay变量中的数据在3秒后显示,但建议(在循环中)不是,它将立即显示此代码不可能,问题一定在其他地方。除了replay
和$replay
?结果是数组吗?@devnull69-两者都是相同的。它实际上是$replay。我错过了$replay。它只是message.php中数组的一个变量