【Angualr4】跨方法计算

本文探讨了TypeScript中遇到的一个具体问题:如何在不同方法间进行全局变量的有效计算与累积。通过示例代码展示了在特定场景下(如考试与培训通知处理)实现积分与学习时间累计的具体实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台TS辛辛苦苦写的TS代码,但是不能够跨方法计算,即使声明的是全局变量也不行,暂时放这里,作为个资料库 ,方便以后自己调用;(注:这里是半成品,不可全部copy)

                 this.examStudyTimeCount=0;
                 this.examPointCount=0;
                 this.examPntCntThsYr=0;
                 this.examHourStudy=0;
                 for(var i=0;i<this.messagenotices.length;i++){
                    /!*考试*!/
                    if(1==this.messagenotices[i].status) {
                        var examId=this.messagenotices[i].examId;
                        this.trainService.findExamUser(examId).subscribe(result2=>{
                            this.examUserList=result2;
                            console.log(result2);

                            var examUser=this.examUserList[0];
                            var exam=this.examUserList[0].exam;
                            if (2==examUser.status) {
                                this.examStudyTimeCount += exam.duration;
                                this.examHourStudy = this.examStudyTimeCount / (60);
                                this.examPointCount += examUser.integral;
                                console.log("考试积分:");
                                console.log(examUser.integral);
                                var timeStudy = new Date(examUser.handInTime);
                                var time00001 = new Date(examUser.handInTime);
                                time00001.setMonth(0);
                                time00001.setDate(0);
                                time00001.setHours(0);
                                time00001.setMinutes(0);
                                time00001.setMilliseconds(1);
                                var now1 = new Date();
                                if (timeStudy < now1 && timeStudy > time00001) {
                                    this.examPntCntThsYr += examUser.integral;
                                }
                            }
                        });
                    }
                    /!*warn 培训*!/
                    if(2==this.messagenotices[i].status){
                        this.studyTimeCount=0;
                        this.pointCount=0;
                        this.studyTimeOnce=0;
                        this.pntCntThsYr=0;
                        this.hourStudy=0;
                        this.train=this.messagenotices[i].train;

                        this.pointCount +=  this.train.point;
                        console.log("培训积分:");
                        console.log(this.pointCount);

                        this.trainService.findTrainMarksAndTime(this.train.id).subscribe(result3=>{
                            this.trainList=result3;
                            var trainInfo = this.trainList[0];
                            var st: any = new Date(trainInfo.startDate);
                            var et: any = new Date(trainInfo.endDatess);
                            this.studyTimeOnce = parseInt((et - st).toString());
                            this.studyTimeCount += this.studyTimeOnce;
                            this.hourStudy = Math.round(this.studyTimeCount / (1000 * 60 * 60));

                            var timeStudy = new Date(trainInfo.endDatess);
                            var time00001 = new Date(trainInfo.endDatess);
                            time00001.setMonth(0);
                            time00001.setDate(0);
                            time00001.setHours(0);
                            time00001.setMinutes(0);
                            time00001.setMilliseconds(1);
                            var now1 = new Date();//获取现在时间;
                            if (timeStudy < now1 && timeStudy > time00001) {
                                this.pntCntThsYr += this.train.point;
                            }
                        });
                    }
           }
               /*this.hourFinal=this.hourStudy+this.examHourStudy;
               this.pointFinal=this.pointCount+this.examPointCount;//6console.log("最终积分");
                console.log(this.pointFinal);
               this.pntCnYrFinal=this.pntCntThsYr+this.examPntCntThsYr;*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶洲川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值