+
95
-

微信小程序request请求如何设置并发限制?

微信小程序request请求如何设置并发限制?

比如最大并发为5,不能超过5个,超过的就得排队。

网友回复

+
15
-

可以参考一下html代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" />
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/axios-0.18.js"></script>
    <script type="text/javascript">
        async function asyncPool(poolLimit, array, iteratorFn) {
              const ret = []; // 存储所有的异步任务
              const executing = []; // 存储正在执行的异步任务
              for (const item of array) {
                // 调用iteratorFn函数创建异步任务
                const p = Promise.resolve().then(() => iteratorFn(item, array));
                ret.push(p); // 保存新的异步任务
      ...

点击查看剩余70%

+
15
-
var req = (function(){
    var count = 0;
    var counter = function() {
        count--;
    };
    return function(fn) {
        if (count < 10) {
            count++;
            fn(count...

点击查看剩余70%

+
15
-

写一个类

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    
  </head>
  <body>
    <script>
      class LimitRequest {
        // 限制同时请求的数量
        limitCount = 0
        // 缓存还没有发送的请求
        cacheRequest = []
        // 当前正在请求的数量
        currentRequest = 0
        constructor(limitCount) {
          this.limitCount = +limitCount || 3
        }
        /**
         * 新增请求
         * @param {*} fn
         */
        addRequest(fn) {
          if (typeof fn !== 'function') {
            console.log('addRequest添加请求需要传入函数')...

点击查看剩余70%

我知道答案,我要回答