简体   繁体   English

返回“void”类型参数的打字稿不能分配给“TimerHandler”类型的参数?

[英]Typescript returning Argument of type 'void' is not assignable to parameter of type 'TimerHandler?

I want to update the count of number of message received by use on live server in angular 7 like we have in facebook and linkedin on main page and i have tried the following code:-我想更新在 angular 7 的实时服务器上使用收到的消息数量,就像我们在 Facebook 和主页上的 Linkedin 一样,我尝试了以下代码:-

the code to fetch message:-获取消息的代码:-

getMessageCount(rid:any)
    {
      return this.http.get<any>(this.apiUrl+'message/'+rid);
    }
on the main page we have the following code run like this :-
export class HomeComponent implements OnInit {
   userAuthenticated=false;
   private tmessage:any;
  constructor(private service:LoginService,private mService:MessagingService) {
    if(this.service.currentUserValue)
    {
      this.userAuthenticated=true;
      const id=this.service.currentUserValue.id;
      window.setInterval(this.getMessages(id),5);

    }
  }
}

upon sending the new message it should be updated without reloading the page ,can anybody tell me what i can do to achieve it ?发送新消息后,它应该在不重新加载页面的情况下进行更新,有人能告诉我我能做些什么来实现它吗?

You didn't provide getMessages , so I have to guess.您没有提供getMessages ,所以我必须猜测。

My guess is, getMessages is a void function, doing something, probably updating model/view and not returning anything, while the first parameter of setTimeout should be a function.我的猜测是, getMessages是一个空函数,在做一些事情,可能更新模型/视图而不返回任何东西,而setTimeout的第一个参数应该是一个函数。 Therefore, this因此,这

      window.setInterval(this.getMessages(id), 5);

should be应该

      window.setInterval(() => this.getMessages(id), 5);

The problem is setInterval() needs a function as the first argument(also called as a callback function).问题是setInterval()需要一个函数作为第一个参数(也称为回调函数)。 Then it executes that function after the set amount of time.然后它在设定的时间后执行该函数。 And also not only run the function once as setTimeout() but regularly after the given interval of time.并且不仅以setTimeout()运行该函数一次,而且在给定的时间间隔后定期运行该函数。 But you are not giving a function in your code但是你没有在你的代码中给出一个函数

window.setInterval(this.getMessages(id), 5);

but just giving a value return by the function getMessages(id) .但只是通过函数getMessages(id)给出一个值返回。

So the correct way should be所以正确的方法应该是

window.setInterval(() => this.getMessages(id), 5);

which now gives a function that can be executed after the given amount of time.现在给出了一个可以在给定时间后执行的函数。

And there is another method where you can call the function by function.call() .还有另一种方法,您可以通过function.call()调用该函数。

window.setInterval(() => this.getMessages.call(this, id), 5)

call() method calls a function with a given this value and arguments provided individually. call()方法使用给定的this值和单独提供的参数调用函数。

Hope this will help you.希望这会帮助你。 (Please correct me if there is anything wrong. Thanks in Advance) (如果有任何错误,请纠正我。提前致谢)

暂无
暂无

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

相关问题 TypeScript 错误 '(response) =&gt; void' 类型的参数不可分配给'(value: void) =&gt; void | 类型的参数承诺喜欢<void> '</void> - TypeScript error Argument of type '(response) => void' is not assignable to parameter of type '(value: void) => void | PromiseLike<void>' 类型“ void”的参数不能分配给“功能”类型的参数 - Argument of type 'void' is not assignable to parameter of type 'Function' Typescript:a 类型的参数不能分配给 b 类型的参数 - Typescript: argument of type a is not assignable to parameter of type b Typescript 错误“类型参数不可分配给类型参数” - Typescript error 'argument of type is not assignable to parameter of type' 设置组件状态时出现 TypeScript 错误:“X”类型的参数不可分配给“() =&gt; void”类型的参数 - TypeScript error when setting a component's state: Argument of type 'X' is not assignable to parameter of type '() => void' “(事件:字符串)=&gt; void”类型的参数不可分配给“EventListenerOrEventListenerObject”类型的参数 - Argument of type '(event: string) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject “(e:CustomEvent)=&gt; void”类型的参数不可分配给“EventListenerOrEventListenerObject”类型的参数 - Argument of type '(e: CustomEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject' TypeScript:类型参数不可分配 - TypeScript: Argument of type is not assignable Typescript 类型 boolean 不可分配给 void - Typescript type boolean is not assignable to void typescript:类型&#39;any []&#39;的参数不能分配给&#39;[]&#39;类型的参数.ts(2345) - typescript : Argument of type 'any[]' is not assignable to parameter of type '[]'.ts(2345)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM