我正在尝试编写一个测量另一个函数执行时间的函数:
export class Profiler {
public measureSyncFunc(fn: () => any): Promise<number> {
return new Promise<number>((resolve, reject) => {
let elapsed = 0;
let intervalId = window.setInterval(() => {
elapsed += 1; // this is never called
}, 1);
this.execFunc(fn)
.then((result: any) => {
window.clearInterval(intervalId);
resolve(elapsed);
});
});
}
private execFunc(fn: () => any): Promise<any> {
return new Promise<any>((resolve, reject) => {
resolve(fn());
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后我就这样使用它:
let array = generateRandomArray(100000);
instance.measureSyncFunc(bubbleSort(array))
.then((elapsed: number) => {
console.log(`end session: ${elapsed} seconds`);
resolve(); …Run Code Online (Sandbox Code Playgroud) 我是Node的初学者,所以也许我的问题可能很天真,抱歉.
我和Grunt一起创建了我的第一个项目.
Grunt使用Node,所以现在node_modules我的项目根目录中有一个很好的文件夹,里面有几个模块.一切都很好.
我加入node_modules/*了我.gitignore,假设我可以从另一个地方轻松地重建它.
然后我回家,用git检索我的项目,现在我不知道如何重建节点模块.
是否有npm可读的命令package.json?
我需要在按钮的点击事件上附加一个异步行为。我想让浏览器先打开带有链接的新标签页,当我返回上一页时,然后执行异步操作。
起初,我测试了这个: window.setTimeout(() => action(), 0);
它在 Chrome 中运行良好,但在 Firefoxaction()中在链接打开之前执行。很坏。我不敢相信 Firefox 像管理同步块一样管理它?
所以我试过了 window.setTimeout(() => action(), 1);
现在它起作用了!
1 毫秒在这里有什么不同,或者内部事件循环中有解释吗?
你知道答案吗 ?(以及为什么它在 Chrome 和 Firefox 之间的管理方式不同?)
我有一个带有从表单接收值的方法的组件。我想用一个接口来描述表单数据。
但是,在运行时(ng serve),编译器告诉我接口未知。( Public property 'friendshipFormModel' of exported class has or is using private name 'IFriendshipFormModel'.)
如何正确声明接口?如果可能,我会避免仅为此接口创建单独的文件,因为它属于组件。
文件:
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import * as moment from 'moment';
import { FriendshipModel } from '../models/friendship.model';
interface IDatePickerDateModel {
day: string;
month: string;
year: string;
formatted: string;
momentObj: moment.Moment;
}
interface IFriendshipFormModel {
name: string;
meetingDate?: IDatePickerDateModel;
}
@Component({
selector: 'app-create-friendship',
templateUrl: './create-friendship.component.html',
styleUrls: ['./create-friendship.component.css']
})
export class CreateFriendshipComponent {
@Output() friendshipCreated = new EventEmitter<FriendshipModel>();
friendshipFormModel: …Run Code Online (Sandbox Code Playgroud) 在插入/更新我的数据库之前,我使用 ajv 来验证 JSON 数据模型。
今天我想使用这个结构:
const dataStructure = {
xxx1234: { mobile: "ios" },
yyy89B: { mobile: "android" }
};
Run Code Online (Sandbox Code Playgroud)
我的键是动态的,因为它们是 id。你知道如何用ajv验证它吗?
PS:作为替代解决方案,我当然可以使用这种结构:
const dataStructure = {
mobiles: [{
id: xxx1234,
mobile: "ios"
}, {
id: yyy89B,
mobile: "android"
}]
};
Run Code Online (Sandbox Code Playgroud)
然后我将不得不在数组上循环以找到我想要的 id。我所有的查询都会变得更加复杂,这让我很困扰。
感谢您的帮助 !
我有一个Typescript应用程序。我将localstorage用于开发目的是为了存储我的对象,而在反序列化时遇到了问题。
我有一个类型为MeetingModel的对象会议:
export interface MeetingModel {
date: moment.Moment; // from the library momentjs
}
Run Code Online (Sandbox Code Playgroud)
我使用将该对象存储在localStorage中JSON.stringify(meeting)。
我假设stringify调用moment.toJson()返回一个iso字符串,因此存储的值是:{"date":"2016-12-26T15:03:54.586Z"}。
检索此对象时,我将执行以下操作:
const stored = window.localStorage.getItem("meeting");
const meeting: MeetingModel = JSON.parse(stored);
Run Code Online (Sandbox Code Playgroud)
问题是:Meeting.date包含一个字符串而不是一刻!
因此,首先我想知道为什么TypeScript会发生这种情况?为什么我可以分配一个字符串值而不是Moment并且编译器同意?
其次,如何将我的对象从纯JSON对象(也称为字符串)还原为Typescript类型?
我当然可以创建一个工厂,但是当我的对象数据库长大时,完成所有这些工作会很麻烦。
也许首先有一个更好地存储在本地存储中的解决方案?
谢谢
我是Redis的新手,并且有一个与备份有关的问题。
现在,我有一个实例正在Windows服务器上运行。在此实例内部,我目前有一个“作业”,可将数据存储在一个数据库中。我不希望备份这些数据。
我必须创造一份新工作。我的第一个想法是将数据存储在另一个数据库中,但是在同一实例上。然后,我将在此数据库ID上激活RDB备份。
但是,当我阅读redis文档时,会看到以下命令进行备份:
save <seconds> <changes>
Run Code Online (Sandbox Code Playgroud)
此命令仅备份当前实例的所有数据库。我看不到针对特定实例,端口或数据库的任何选项设计。您知道我如何隔离两个作业,以便仅备份第二个作业吗?
我的边界是我只有一台服务器。
感谢大伙们 !
javascript ×3
typescript ×3
json ×2
node.js ×2
ajv ×1
angularjs ×1
es6-promise ×1
git ×1
gruntjs ×1
redis ×1