简体   繁体   English

如何在Vuejs中实现节流方法?

[英]How to implement a throttle method in Vuejs?

Currently I'm working inside a chat application using webSockets. 目前,我正在使用webSockets开发聊天应用程序。 I've implemented a indicator which shows that one of the chat members is currently typing. 我已经实现了一个指示器,该指示器表明当前有一个聊天成员正在键入。 I have another call which fires 5 seconds after the person starts typing to remove the indicator. 我有另一个电话,该电话会在该人开始键入以删除指示符后5秒钟触发。 The problem I'm having is that if the person continues to type longer than 5 seconds then the 'typing' indicator flashes in and out rapidly in the UI... Here is my current implementation. 我遇到的问题是,如果该人继续键入超过5秒,那么“类型”指示器将在UI中快速闪烁...这是我当前的实现方式。

sendChatState({ commit, dispatch, state }, payload) {
    connectionService.connection.setDialogChatState({dialogId: payload.visitId, conversationId: payload.visitId, chatState: 'composing'})
    // Create set timeout here to pause typing indicator...
    setTimeout(() => {
    connectionService.connection.setDialogChatState({dialogId: payload.visitId, conversationId: payload.visitId, chatState: 'pause'})
    }, 5000)
  },

Appears I may need to use some type of throttle to limit the amount of calls. 看来我可能需要使用某种类型的油门来限制通话量。 However this is where I'm having trouble as I'm not entirely sure how to implement this. 但是,这是我遇到麻烦的地方,因为我不确定如何实现。

You can solve it using debounce from lodash. 您可以使用lodash的反跳来解决它。 Doing: 这样做:

_.debounce(callServiceMethod, delay_time, { 'leading': true })

The debounce function is sending you back a throttled version of the function when you are calling it. 防反跳功能会在您调用它时将您的函数的受限制版本发送回给您。 In order to access it from the component scope and in the template within an eventHandler, the easiest way would be something like this: 为了从组件范围和eventHandler的模板中访问它,最简单的方法是这样的:

import debounce from 'lodash/debounce'

export default {
  ...,
  methods: {
    sendChatState: debounce(function() {
            do your thing here 
    }, delay_time, { 'leading': true }),
  },
  ...
}

Note you need to use the config parameter to immediate call the method. 请注意,您需要使用config参数立即调用该方法。

More info here . 更多信息在这里

Let's set the flag typing to FALSE at the beginning. 让我们设置标志typing为FALSE开头。 On each change of the input value (meaning there is typing) you start a timer (and first cancel the previous timer, if there was an active one) and if the flag typing was FALSE - set it to TRUE and show the indicator (otherwise it has already been shown). 输入值的每次更改(意味着有输入)都会启动一个计时器(如果有活动的计时器,则首先取消上一个计时器),并且如果typing的标志为FALSE-将其设置为TRUE并显示指示器(否则它已经显示)。 Once the timer fires - you hide the indicator and set the flag typing to FALSE. 一旦定时器触发-你隐藏的指示灯和标志设置typing为FALSE。

<template>
  <textarea v-model="message" @input="setFlag"/>
</template>

<script>
...
data()
{
  return {
    message: '',
    typing: false,
    timer: null,
  }
},
beforeDestroy()
{
  if (this.timer) clearTimeout(this.timer);
},
methods:
{
  setFlag()
  {
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(this.timeOut, 5000);
    if (!this.typing)
    {
      this.typing = true;
      connectionService.connection.setDialogChatState({dialogId: payload.visitId, conversationId: payload.visitId, chatState: 'composing'});
    }
  },
  timeOut()
  {
    this.typing = false;
    connectionService.connection.setDialogChatState({dialogId: payload.visitId, conversationId: payload.visitId, chatState: 'pause'});
    this.timer = null;
  }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM