Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript Angular2在一个时间间隔内没有Http请求_Typescript_Angular_Intervals_Ionic2 - Fatal编程技术网

Typescript Angular2在一个时间间隔内没有Http请求

Typescript Angular2在一个时间间隔内没有Http请求,typescript,angular,intervals,ionic2,Typescript,Angular,Intervals,Ionic2,我是新来的。 我有一个绑定到用户交互的值,我需要通过http请求发送这个值。我在值更改时触发了一个事件,但该值每秒可以更改多次。 我想限制http请求:当用户交互时,每2秒1个。 我不想在没有交互的情况下发送http请求。 所以我想用定时器或间隔来做,但我不知道用什么角度来做: onChange() { if(_interval.timerFinished) { http.get(.............); _interval.launchTimer(2000)

我是新来的。 我有一个绑定到用户交互的值,我需要通过http请求发送这个值。我在值更改时触发了一个事件,但该值每秒可以更改多次。 我想限制http请求:当用户交互时,每2秒1个。 我不想在没有交互的情况下发送http请求。 所以我想用定时器或间隔来做,但我不知道用什么角度来做:

onChange() {

  if(_interval.timerFinished) {
      http.get(.............);
      _interval.launchTimer(2000); //2 seconds
  }

}
也许用这个:

怎么样

您也可以先尝试Sample或ThrottleFirst

http.get(……..).debounce(2000)

怎么样

您也可以先尝试Sample或ThrottleFirst

http.get(……..).debounce(2000)

我想限制http请求

为此,您可以通过在html元素中添加以下内容来绑定元素:

[(ngModel)] = "searchQuery" [ngFormControl]="searchQueryControl"
然后在代码中:

// Remember to import these things:
import {FORM_DIRECTIVES, Control} from 'angular2/common';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

constructor ( ... ) {
    //...
    this.searchQuery = '';
    this.searchQueryControl = new Control();
    //...

    this.searchQueryControl
        .valueChanges
        .debounceTime(1000)
        .distinctUntilChanged()
        .subscribe(text => {

            if(text !== '') {
              this.searchQuery = text;

              // Do whatever you want with the text
            }
      });

    //...
}
通过这样做,我们只会在输入发生更改(
this.searchQueryControl.valueChanges
)和1秒内没有其他更改(
.debounceTime(1000)
)时运行代码。
distinctUntilChanged()
允许我们在值与上次运行时不同的情况下运行代码。因此,如果用户键入'asd',点击退格键,然后再次键入结尾'd',则不会发生任何事情

我想限制http请求

为此,您可以通过在html元素中添加以下内容来绑定元素:

[(ngModel)] = "searchQuery" [ngFormControl]="searchQueryControl"
然后在代码中:

// Remember to import these things:
import {FORM_DIRECTIVES, Control} from 'angular2/common';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

constructor ( ... ) {
    //...
    this.searchQuery = '';
    this.searchQueryControl = new Control();
    //...

    this.searchQueryControl
        .valueChanges
        .debounceTime(1000)
        .distinctUntilChanged()
        .subscribe(text => {

            if(text !== '') {
              this.searchQuery = text;

              // Do whatever you want with the text
            }
      });

    //...
}

通过这样做,我们只会在输入发生更改(
this.searchQueryControl.valueChanges
)和1秒内没有其他更改(
.debounceTime(1000)
)时运行代码。
distinctUntilChanged()
允许我们在值与上次运行时不同的情况下运行代码。因此,如果用户键入“asd”,按backspace键,然后再次键入结尾的“d”,则不会发生任何事情。

不幸的是,我的用户交互使用的是复杂的旋钮/范围拖放指令。所以没有输入应用FormControl
knobVolumeValueChanged(newValue){this.knobVolumeValue=newValue//需要对this.http.get(..).map(res=>res.json()).subscribe(data=>{});}
不幸的是,我的用户交互使用的是一个复杂的旋钮/范围拖放指令。所以没有输入应用FormControl
knobVolumeValueChanged(newValue){this.knobVolumeValue=newValue//需要对this.http.get(..).map(res=>res.json()).subscribe(data=>{};}