0

I want to cancel running request by clicking button

When http request call then loader is running but most of the time request taking longer time so that I want to cancel that request while running

here is my html code

<div *ngIf="progress.state | async; let state">
    <div class="loading-icon" *ngIf="state.active">
        <button class="btn btn--primary" (click)="cancelRequest()">Cancle Request</button>
    </div>
</div>

here is my loader.ts file

import { HttpModule, BrowserXhr } from '@angular/http';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgProgressModule, NgProgressBrowserXhr } from 'ngx-progressbar';
import { SpinnerComponent } from './spinner.component';

@NgModule({
  imports: [
    CommonModule,
    HttpModule,
    NgProgressModule
  ],
  declarations: [
    SpinnerComponent
  ],
  providers: [
      { provide: BrowserXhr, useClass: NgProgressBrowserXhr },
  ],
  exports:[
    SpinnerComponent
  ]
})
export class PageloaderModule {
  cancelRequest(){

  }
 }

I'm trying to set a Boolean variable in http interceptor but not working

So how can I cancel running http request

NIKHIL RANE
  • 4,012
  • 2
  • 22
  • 45
  • 1
    I don't see a HTTP request in your code. What about storing the subscription returned from the `http.get()`, `http.post()`, ... call and call `unsubscribe()` when you don't want to wait for a response anymore? – Günter Zöchbauer Feb 16 '18 at 07:09
  • I want to cancel request when the loader is running and loader is run when the http request is call any ware from the project – NIKHIL RANE Feb 16 '18 at 07:12
  • You need to either unsubscribe or throw an error (possibly RxJS TimeoutError) at some point. You're using Http and not HttpClient. There are no interceptors in Http. – Estus Flask Feb 16 '18 at 07:53
  • As for HttpClient, see https://stackoverflow.com/questions/45938931/angular-httpclient-default-and-specific-request-timeout . I guess that instead of `.timeout(timeoutObservable)` you will need `.merge(cancelSubject)` and then you can do `cancelSubject.error(new TimeoutError)` anywhere. – Estus Flask Feb 16 '18 at 08:02

0 Answers0