我正在努力修改这部分代码。使用了setTimeout和.delay()。这段代码的作用是回答用户输入的问题。它立即做到了,我想让它看起来更自然,并添加人类像答案延迟。
// Form submission - This is where the magic happens!
$('#talkform').submit(function(e) {
e.preventDefault();
var bot_name = $('#sbBot_id option:selected').text();
var user = $('#say').val();
var userSaid = userTitle + 'User: ' + endSpan + user + "<br>\n";
$('#chatlog').html($('#chatlog').html() + userSaid);
var botSaid = botTitle + "Admin: " + endSpan;
var formdata = $("#talkform").serialize();
$('#say').val('')
$('#say').focus();
$.post('<?php echo $url ?>', formdata, function(data){
var b = data.botsay;
if (b.indexOf('[img]') >= 0) {
b = showImg(b);
}
if (b.indexOf('[link') >= 0) {
b = makeLink(b);
}
var usersay = data.usersay;
$('#chatlog').html($('#chatlog').html() + botSaid + b + "<br>\n");
}, 'json').fail(function(xhr, textStatus, errorThrown){
$('#urlwarning').html("Something went wrong! Error = " + errorThrown);
});
return false;
});
答案 0 :(得分:0)
浏览delay()的整个文档,您可以指定时间(以秒为单位),让它更具人性化感。
ex:$( "#foo" ).slideUp( 300 ).delay( 4000 ) \\4 seconds delay
.fadeIn( 400 );
\ 4毫秒延迟
答案 1 :(得分:0)
将字符串&#34; $(&#39; #chatlog&#39;)。html(...&#34;放入setTimeout处理函数中,如下例所示(2000表示2s延迟):
$(document).ready(function(e){
$("#submitbtn").click(function(e) {
e.preventDefault();
// the code to receive answer
setTimeout(function(){
$("#answer").html("The answer is here");
}, 2000);
});
});
&#13;
.asnwer {
padding: 4px;
border: solid 1px gray;
margin-bottom: 8px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class=asnwer id=answer> </div>
<input id=submitbtn type=button value=submit>
&#13;