res

异步编程-订单模式

本小妞迷上赌 提交于 2019-12-04 18:10:29
//0:在排队 1:处理中 2 订单完成 3 订单取消 4 超时处理 const ShopOrderMap={} class Order { constructor(name){ this.name=name||'test'; if(!ShopOrderMap[name]){ ShopOrderMap[name]={ bit:[], curN:0, orderMap:{}//待处理的订单 }; } this.shopOrder=ShopOrderMap[this.name]; //新增加一个订单 this.myN=this.shopOrder.bit.length; this.shopOrder.bit[this.myN]='0'; this.shopOrder.orderMap[this.myN]=this; } //排队 wait(overTime){ return new Promise((res)=> { this.res=res; //没有人排队,处理当前订单 this.resolve() }) if(overTime){ this.timeInter=setTimeout(()=>{ this.reject() },overTime) } } //排队处理订单 resolve(){ if(this.myN===this.shopOrder.curN){ if(this

python中字典

夙愿已清 提交于 2019-12-04 05:31:11
字典中key:不可改变的数据类型 #fromkeys 快速定义一个空字典 res = {}.fromkeys(['a','b','c'],['1','2','3']) print(res) 定义字典: dict1 = { 'name1':'天明', 'age':'25', 'high':'170' } dict2 = { 'name2':'tian', 'age':'25', 'phone':'100' } #[ ] 根据key取值 如果取不到报错 >>> dict1 = { ... 'name1':'天明', ... 'age':'25', ... 'high':'170' ... } >>> res = dict1['name1'] >>> print(res) 天明 res = dict1['name11'] #报错 print(res) #get 根据key取value 如果取不到则返回None res = dict1.get('name') print(res) #update 一般用来合并字典 dict1.update(dict2) print(dict1) #打印字典里所有的值 print(dict1.values()) #打印字典里所有的keys print(dict1.keys()) #打印字典里所有的键值对 print(dict1.items()) #pop

自定义express中间件

爱⌒轻易说出口 提交于 2019-12-04 01:23:11
const http = require('http') class LikeExpress { constructor() { this.middleList = [] this.routes = { all: [], get: [], post: [] } } // 处理参数 register(path) { const info = {} const slice = Array.prototype.slice if (typeof path === 'string') { info.path = path info.stack = slice.call(arguments, 1) } else { info.path = '/' info.stack = slice.call(arguments, 0) } return info } use() { const info = this.register.apply(this, arguments) this.routes.all.push(info) } get() { const info = this.register.apply(this, arguments) this.routes.get.push(info) } post() { const info = this.register.apply(this,

ajax语法

别等时光非礼了梦想. 提交于 2019-12-03 22:46:59
$.ajax({ type: 'POST', url: "url", data: { appName: appName, op: appStatus }, dataType: "JSON", success:function(res) { console.log(res); $("#status"+appId).text(res.strStatus); $("#btn"+appId).text(res.strBtn); $("#light"+appId).css("background-color",res.light); } }); 来源: https://www.cnblogs.com/ushowtime/p/11810089.html

统计某字符串出现的次数

半世苍凉 提交于 2019-12-03 21:45:00
const str = 'aaasdofjaopfjopaiiisjssfopiasdfffff' ; /*--------- '连续' 出现最多的字母及出现的次数 ------*/ function findRepeat ( str ) { const arr = str . match ( /(.)\1+/g ) let [ maxKey , max ] = [ '' , 0 ] arr . map ( item => { if ( item . length > max ) { maxKey = item [ 0 ] ; max = item . length ; } ; } ) ; return { maxKey , max } } ; const res = findRepeat ( str ) console . log ( '连续出现次数最多的是' + res . maxKey + '出现了' + res . max + '次数' ) /*--------- 出现最多的字母及出现了多少次 ------*/ function findRepeat ( str ) { let [ maxKey , max , obj ] = [ '' , 1 , { } ] ; for ( var i = 0 ; i < str . length ; i ++ ) { obj [ str

宠物收养所

泪湿孤枕 提交于 2019-12-03 20:50:24
https://loj.ac/problem/10144 题目描述   宠物收养所同一时间只可能存在宠物或收养者。若收养者过多,则会选择收养者中 \(|a-b|\) 中最小的,若存在两个则取特征值小的那个。定义不满意度为 \(\sum |a-b|\) ,求不满意度的总和。 思路   我们考虑由于同一时刻最多只可能有收养者或宠物,所以我们只要记录一下当前收养所内是收养者还是宠物,相同则新建节点,不相同就删除节点并累加答案。 代码 #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll N=8e4+10; const ll INF=0x3f3f3f3f; const ll mod=1e6; struct Treap { ll lc,rc,cnt,v,w; }a[N]; ll root,sum; void zig(ll &k) { ll t=a[k].lc; a[k].lc=a[t].rc; a[t].rc=k; k=t; } void zag(ll &k) { ll t=a[k].rc; a[k].rc=a[t].lc; a[t].lc=k; k=t; } void insert(ll &k,ll key) { if(!k) { k=++sum; a[k].lc=a[k].rc=0; a[k

js数组去重

南楼画角 提交于 2019-12-03 14:59:28
方法一:js数组id去重,value值相加问题 来源:https://www.jianshu.com/p/8f79e31b46ed // js let arr = [ { id: 1, value: 5 }, { id: 2, value: 3 }, { id: 3, value: 4 }, { id: 1, value: 3 }, { id: 2, value: 3 } ]; let newArr = []; arr.forEach(el=> { const res = newArr.findIndex(ol=> { return el.id === ol.id; }); if (res!== -1) { newArr[res].value = newArr[res].value + el.value; } else { newArr.push(el); } }); 问题:原来的数组arr中的数据也改变了。怎么做才能让原来的数组保持不变呢? 解决: js 数组的深度拷贝 的四种实现方法 来源https://www.cnblogs.com/juneling/p/9149969.html 适用 多层 数组嵌套的深拷贝 var ary2 = JSON.parse(JSON.stringify(ary1));   //此方法适用于Oject的深度拷贝,因为Array属于Oject类型

Nodejs仿Apache的功能

匿名 (未验证) 提交于 2019-12-03 00:40:02
一、初步实现Apache功能 // 1.加载模块 var http = require (‘ http ‘ ); var fs = require (‘ fs ‘ ); // 2.创建server var server = http . createServer (); // 3.监听server的request请求事件,设置请求处理函数 var wwwDir =‘ C : /Users/ Administrator / Desktop / Code ‘; // 设置目录(注意转义符) server . on (‘ request ‘, function ( req , res ){ var url = req . url ; if ( url ===‘/‘ ){ fs . readFile ( wwwDir +‘/ index . html ‘, function ( error , data ){ if ( error ){ // return返回值,阻止代码往后执行 return res . end (‘ 404 Not Found ‘ ); } res . end ( data ); }); } else if ( url ===‘/ a . txt ‘ ){ fs . readFile ( wwwDir +‘/ a . txt ‘, function ( error ,

上传文件iView组件

匿名 (未验证) 提交于 2019-12-03 00:34:01
uploadSuccess2 (res, file) { //上传成功 this.$Message.info(res.msg); if(res.code == 200){ this.excel_name = res.info.originalName; } } uploadError(error, file) { //上传失败 this.$Message.info(error); } handleFormatError2 (file) { this.$Message.info("文件格式不正确,请上传excel格式文件"); } uploadError(error, file) { this.$Message.info(error); } 文章来源: 上传文件iView组件

node.js Express 创建RESTful API

匿名 (未验证) 提交于 2019-12-03 00:34:01
0.项目机构 1.package.js 安装mongoose依赖和body-parser依赖 { "name": "test", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "body-parser": "^1.18.3", "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", "http-errors": "~1.6.2", "mongoose": "^5.1.6", "morgan": "~1.9.0", "pug": "2.0.0-beta11" } } npm install mongoose --save npm install body-parser --save 2. app.js 加数据库连接,还有router 这边在mongodb中创建了一个xiaohua1的database var mongoose = require('mongoose'); mongoose.connect('mongodb://article:123456@localhost:27017/xiaohua1') var apiRouter = require('.