在某个云函数,写了如下代码:
console.log( "timer start" ) setTimeout( function () { console.log( "timer end" ) const tdb = cloud.database() tdb.collection( 'test' ).add({ data: { time: db.serverDate() } }).then(res => { if (res.errMsg.indexOf( 'ok' ) == -1) { console.log(res) console.log( "add test failed." ) return null } }) }, 10000) |
期望10秒后执行添加记录到test数据库的操作,但是10秒后代码并没有执行。
等到20秒后某个来自小程序端的请求来了,此时添加test记录的那段代码就执行了。
这是什么原因呢?
这应该用触发器吧,云函数不会自动执行,除非使用触发器或者从小程序端、其它云函数调用发起。
10秒不到,云函数就运行到结尾了,一到结尾就会关闭进程的,不会等你的timeout.