问个执行顺序方面的小白问题?
发布于 5 年前 作者 fang29 7286 次浏览 来自 问答

为什么代码执行不是按顺序的啊,这就是解释语言,执行慢的晚输出啊!如果想要按顺序来执行怎么弄啊?

预计:

写入数据1

循环1

写入数据2

循环2

写入数据3

循环3

。。。

实际是:

循环1

循环2

循环3

。。。

写入数据11

写入数据11

写入数据11

。。。

aaa=1

    db_total=10

    console.log('1111总数1111: ', db_total)

    while (aaa <= db_total) {

        this.data.markers.push({latitude: 0,longitude: 0,}),

        db.collection(‘wy_gcb’).where({ t0: aaa }).get({

            success: res => {this.setData({‘markers[2].longitude’: res.data[0].t4})

            console.log(‘写入数据’, aaa)

          },})

      console.log('循环: ', aaa)

      aaa++;

    }

3 回复

知道了,谢谢!

Promise 风格 了解一下

await db.collection('wy_gcb').where({t0: aaa}).get({
    success: (res) => {
        this.setData({'markers[2].longitude': res.data[0].t4})
        console.log('写入数据', aaa)
    }
})

这是一个典型的闭包问题,用闭包思维方式来处理即可。

回到顶部