import {Injectable} from '@angular/core';

@Injectable()
export class Load {
    constructor() {
    }

    public isLoad = false;
    public showNum = false;
    public msg = '';
    public time = 0;

    toLoad(msg, type) {
        if (!this.isLoad) {
            // 进行异步操作,避免angular在检查时刚初始化就重新赋值check会报错
            // 虽然无实质影响,但那个红色报错看得我不舒服,就此调成类异步
            setTimeout(() => {
                this.isLoad = true;
                this.msg = msg;
                if (type) {
                    this.showNum = true;
                    this.time = 0;
                    this.addNum();
                }
            });
        }
    }

    addNum() {
        setTimeout(() => {
            if (this.isLoad) {
                this.time++;
                this.addNum();
            }
        }, 1000);
    }

    offLoad() {
        setTimeout(() => {
            this.isLoad = false;
            this.showNum = false;
            this.msg = '';
            this.time = 0;
        });
    }
}
