SHARE
TWEET

Untitled

a guest Jun 20th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. isAfter: boolean;
  2.  
  3.   private interval;
  4.  
  5.   ngOnInit(): void {
  6.     const now = new Date();
  7.     const time = now.setSeconds(now.getSeconds() + 5);
  8.  
  9.     const future = new Date(time);
  10.  
  11.     this.checkTime(future);
  12.   }
  13.  
  14.   private checkTime(time: any): void {
  15.     this.interval = setInterval(() => {
  16.       const now = new Date();
  17.  
  18.       if (now === time) {
  19.         console.log('stop');
  20.         this.isAfter = true;
  21.         clearInterval(this.interval);
  22.  
  23.       } else {
  24.         console.log('next');
  25.         this.isAfter = false;
  26.       }
  27.     }, 1000);
  28.   }
  29. }
  30.      
  31. import { TestBed, async, ComponentFixture, fakeAsync, tick, discardPeriodicTasks } from '@angular/core/testing';
  32. import { AppComponent } from './app.component';
  33.  
  34. describe('AppComponent', () => {
  35.  
  36.   let component: AppComponent;
  37.   let fixture: ComponentFixture<AppComponent>;
  38.  
  39.   beforeEach(async(() => {
  40.     TestBed.configureTestingModule({
  41.       declarations: [
  42.         AppComponent
  43.       ],
  44.     }).compileComponents();
  45.  
  46.  
  47.     fixture = TestBed.createComponent(AppComponent);
  48.     component = fixture.componentInstance;
  49.   }));
  50.  
  51.  
  52.   it ('should check time', fakeAsync(() => {
  53.  
  54.     fixture.detectChanges();
  55.     console.log('isAfter 1', component.isAfter);
  56.  
  57.     tick(3000);
  58.     fixture.detectChanges();
  59.     console.log('isAfter 3', component.isAfter);
  60.  
  61.     tick(6000);
  62.     fixture.detectChanges();
  63.     console.log('isAfter 6', component.isAfter);
  64.  
  65.     discardPeriodicTasks();
  66.  
  67.   }));
  68.  
  69. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top