- 一个下拉框 200条数据 怎么优化 (默认展示10条)
- 60个请求(限制最多同时请求6个)请求并行方案
- 原生拖拽方案及实现细节(mouseMove、drag,drop) ✅ (有待继续完善)
- 数组遍历方法 哪个快
- 手写函数实现数组 。[12, 3, 24, 1, 932, 6423]按照首位排序
- 手写实现add函数 满足 add(1)(2)(3)() 返回 6
2、60个请求(限制最多同时请求6个)请求并行方案
<script>debuggerfunction concurrentPoll() {this.tasks = []; this.max = 10; setTimeout(() => {this.run()}, 2000)}concurrentPoll.prototype.addTask = function (task) { this.tasks.push(task)}concurrentPoll.prototype.run = function () { if (this.tasks.length == 0) { return}const min = Math.min(this.tasks.length, this.max); console.log('min', min)for (let i = 0; i < min; i++) {this.max--; const task = this.tasks.shift(); task().then((res) => { debuggerconsole.log(res)console.log('res', this.max) }).catch((err) => {console.log(err)}).finally(() => { debuggerthis.max++; this.run(); console.log('finally', this.max)})}}const poll = new concurrentPoll(); for (let i = 0; i < 13; i++) { poll.addTask(function () {return new Promise(function (resolve, reject) {resolve(i + '成功') })})}</script>
3、原生拖拽方案及实现细节(mouseMove、drag,drop)
<script>
export default {name: 'drag',data() {return {afterData: ['', '', '', ''],beforeData: ['花', '好', '月', '圆'],resData: [['花', '好', '月', '圆'],['百', '年', '好', '合'],['一', '帆', '风', '顺']],beforeId: '',dragId: '',downMoveX: 0, downMoveY: 0,mouseX: 0, mouseY: 0,}},mounted() {this.dragId = document.getElementById('drag')this.oldBlanks = document.querySelectorAll('.after')console.log('oldBlanks', this.oldBlanks)console.log('drag-offsetTop', this.dragId.offsetTop)},methods: {handleBeforeDown(e, id) {this.beforeId = document.getElementById(id)console.log('eeee->', e)const { offsetX, offsetY } = ethis.mouseX = offsetXthis.mouseY = offsetYthis.downMoveX = this.beforeId.offsetLeft this.downMoveY = this.beforeId.offsetTopconsole.log('this.downMoveY', this.downMoveY)const setDiv = this.beforeId.stylesetDiv.background = 'pink'setDiv.position = 'absolute'setDiv.top = this.downMoveY + 'px'setDiv.left = this.downMoveX + 'px'setDiv.width = '98px'setDiv.height = '98px'this.beforeId.addEventListener('mousemove', this.handleBeforeMove)console.log('beforeId', this.beforeId)},handleBeforeMove(e){console.log('9023', e)console.log('232', window.event)const { offsetX, offsetY, clientX, clientY } = elet moveY = clientY - this.dragId.offsetTop - this.mouseYlet moveX = clientX - this.dragId.offsetLeft - this.mouseXconsole.log('moveY-->', moveY)console.log('moveX-->', moveX)if (moveY <= 0) {moveY = 0} else if (moveY >= 500) {moveY = 500}if (moveX <= 0) {moveX = 0} else if (moveX >= 500) {moveX = 500}this.beforeId.style.top = moveY + 'px'this.beforeId.style.left = moveX + 'px'},handleBeforeUp() {if (this.beforeId) {this.handleDIvAdsorption()this.beforeId.removeEventListener('mousemove', this.handleBeforeMove)this.beforeId = ''this.mouseX = 0this.mouseY = 0this.downMoveX = 0 this.downMoveY = 0}},handleDIvAdsorption() {const whiteDivArea = this.oldBlanks[0].offsetWidth * this.oldBlanks[0].offsetHeightfor (let i = 0;i < this.oldBlanks.length; i++) {if (this.afterData[i] !== '') {continue;}const oldDiv = this.oldBlanks[i]const { offsetTop, offsetLeft } = this.beforeIddebuggerlet verticalLength = oldDiv.offsetHeight - ( offsetTop - oldDiv.offsetTop )console.log('verticalLength', verticalLength)let transverseLength = 0if (offsetLeft >= oldDiv.offsetLeft) {transverseLength = oldDiv.offsetWidth - (offsetLeft - oldDiv.offsetLeft)} else {transverseLength = oldDiv.offsetWidth - (oldDiv.offsetLeft - offsetLeft)}if (verticalLength > 0 && transverseLength > 0) {const occupiedArea = transverseLength * verticalLengthconsole.log('transverseLength', transverseLength)if (occupiedArea / whiteDivArea >= 0.5 ) {console.log('在指定区域,可以吸附')this.handleSetMouseDiv(oldDiv.offsetTop, oldDiv.offsetLeft)this.afterData.splice(i, 1, this.beforeId.innerText)return}console.log('不可以吸附')this.handleSetMouseDiv(this.downMoveY, this.downMoveX)}}},handleSetMouseDiv(top, left){const setDiv = this.beforeId.stylesetDiv.top = top + 'px'setDiv.left = left + 'px'}}
}
</script>