zjl15228795793
zjl15228795793
2021-05-15 08:44
采纳率: 100%
浏览 222

count的值永远是0。是因为filter是异步处理的原因吗?

function calcCount(tAry) { var count = 0 var newAry = taskAry.filter(item => item.isVaild == false) count = newAry.length $('#num_td').text(count + 'item left') } function updataTask() { var html = template('taskTpl', { tasks: taskAry }) $('.tdthins').html(html) } var taskAry = [] $.ajax({ type: 'get', url: '/get-task', data: {}, success: function (data) { taskAry = JSON.parse(data) updataTask() calcCount() }

  • 点赞
  • 收藏

5条回答 默认 最新

  • technologist_30
    CSDN专家-张老师 2021-05-15 09:02
    已采纳

    解决问题,而不是换种写法

    点赞 1 评论
  • technologist_32
    CSDN专家-Time 2021-05-15 09:01
    async function ajax(url) {
        return new Promise(function (resolve, reject) {
            let ajaxSetting = {
                url: url,
                success: function (response) {
                    resolve(response);
                },
                error: function () {
                    reject("请求失败");
                }
            }
            $.ajax(ajaxSetting);
     
        });
    }
     
    async function run() {
        let response1 = await ajax("data1.json");
        let response2 = await ajax(response1["url"]);
        let response3 = await ajax(response2["url"]);
        console.log(response3);
    }
     
    //不阻塞
    run();

    参考

    点赞 1 评论
  • technologist_36
    CSDN专家-sinjack 2021-05-15 08:54

    你加个延时看看,把newAry的值console.log

    点赞 评论
  • technologist_30
    CSDN专家-张老师 2021-05-15 08:54

    你检查下taskAry拿到数据了吗,拿到之后看下newAry这个结果,调试下即可

    点赞 评论
  • technologist_32
    CSDN专家-Time 2021-05-15 09:00

    用await 和async字段

    点赞 评论

相关推荐