5分钟后自动刷新

时间:2017-03-21 11:14:38

标签: javascript jquery

我正试图在我的项目上进行自动刷新,这是聊天机器人。当聊天页面打开并加载对话时,我应该定期刷新会话条目列表。 默认情况下,每5秒钟。

  1. 如果刷新时没有其他会话条目,我们应该将等待时间增加5秒,最多120秒。
  2. 如果刷新时有其他会话条目,我们应该将等待时间重置为5秒。

2 个答案:

答案 0 :(得分:0)

您可以将WebSocket用于聊天机器人。你看到https://www.html5rocks.com/es/tutorials/websockets/basics/

答案 1 :(得分:0)

var delayTime = 5000;

    function refresh() {
        if(chatid.length > 0) {
        ChatService.getChatDetailsById(chatid)
            .then(function (result) {
                if (result.Data.Messages.length > $scope.chatDetails.Messages.length) {
                    getChatDetails(chatid)
                    delayTime = 5000;
                } else {
                    increseDelay();
                }
                timeout();
            });
            function increseDelay() {
                if (delayTime !== 120000)
                    delayTime += 5000;
            }
        }
    }
    function timeout() {
        setTimeout(function () {
            refresh();
        }, delayTime);
    };
    timeout();
相关问题