1 回答

TA貢獻1845條經驗 獲得超8個贊
您可以用來setTimeout延遲操作。
另請注意,如果您已在頁面中包含 jQuery,您也可以使用其 AJAX 方法來簡化代碼。嘗試這個:
let $divChat = $('#divChat');
function sendMessage() {
$.post('http://localhost:3000/api/v1/bots/renault/converse/user1', {
type: 'text',
text: $('#userMessage').val()
}, function(response) {
$('#userMessage').val(''); // empty the typed message
let $indicator = $('<div class="typing-indicator"></div>').appendTo($divChat);
setTimeout(() => {
$indicator.remove();
$divChat.append(response + "</br>");
}, 2000);
});
};
另請注意,從對 AJAX 請求的響應來看,您似乎返回了一個不理想的純文本響應,因為它可能會受到空格的影響。我建議您修改服務器端邏輯以返回序列化格式,例如 JSON 或 XML。
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報