[英]Debounce time not working when subscribing to the observable
I want to send passwordAvailable request to server after sometime (3seconds) when the user starts typing.我想在用户开始输入的某个时间(3 秒)后向服务器发送 passwordAvailable 请求。 But it gets sent anyways.
但无论如何它都会被发送。 I have set debounceTime to 3 seconds, still nothing.
我已将 debounceTime 设置为 3 秒,仍然没有。 This is my validator
这是我的验证器
validate = (control: AbstractControl): Observable<ValidationErrors> | null => {
const value = control.value;
return this.authService.passwordAvailable(value, this._username).pipe(
debounceTime(3000),
tap(
()=>console.log('hi')
),
map(value => {
if (value == 'true') {
return null;
}
return { matchNotFound: true };
})
);
}
My Component.ts我的组件.ts
constructor(private fb: FormBuilder,
private authService: AuthService,
private passwordValidators: PasswordValidators,
) {}
ngOnInit(): void {
this.passwordValidators.setUsername(this.username);
this.changePassForm = this.fb.group({
oldPassword : [this.password,
Validators.required,
this.passwordValidators.validate
]
});
}
My Service我的服务
passwordAvailable(password: string, username: string): Observable<any>{
let data = {password: password, username: username};
return this.http.get<any>('url:check-password',{params: data});
}
the service returns string as (true or false)服务将字符串返回为(true 或 false)
Please Help, thanks in advance请帮助,提前谢谢
I even tried, using valueChanges, but service isnt even getting called here, this is what i tried我什至尝试过使用 valueChanges,但这里甚至没有调用服务,这就是我尝试过的
return control.valueChanges.pipe(
debounceTime(3000),
tap(()=>console.log('hi')),
map((val) => {return this.authService.passwordAvailable(val, this._username)}),
map((v: any) => {
if (v == 'true') {
return null;
}
return { matchNotFound: true };
})
)
I changed the code to我将代码更改为
return timer(3000).pipe(
switchMap(() => {
return this.authService.passwordAvailable(value, this._username).pipe(
tap(
()=>console.log('hi')
),
map(value => {
if (value == 'true') {
return null;
}
return { matchNotFound: true };
})
);
})
)
Now api is getting hit after 3 seconds.现在 api 在 3 秒后被击中。
With DebounceTime, i tried changing it to使用 DebounceTime,我尝试将其更改为
return control.valueChanges.pipe(
debounceTime(2000),
switchMap(value=>{
return this.authService.passwordAvailable(value, this._username).pipe(
map(value => {
if (value == 'true') {
return null;
}
return { matchNotFound: true };
})
);
})
)
But still no request sent, so timer was the only option left但仍然没有发送请求,所以计时器是唯一的选择
should be simply应该很简单
this.control.valueChanges.pipe(
debounceTime(300),
switchMap(value=>{
return this.authService.passwordAvailable(value, this._username)
})
).subscribe(res=>{console.log(res)})
That's.那是。 you observer the control changes, but only emit a value if pass 300 miliseconds -debounceTime(300)-.
您观察控件更改,但仅在通过 300 毫秒 -debounceTime(300)- 时发出一个值。 But you don't want the result of change, you want return the value of the service.passwordAvailable -switchMap-
但是你不想要改变的结果,你想要返回 service.passwordAvailable -switchMap- 的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.