微信小程序可以正常使用settimeout吗?

我怎么用都报错,我搞不太懂,
我是要在onReachBottom 时调用settimeout(callback(),5000);
为了显示
代码如下

data: {

hidden:true

},

onReachBottom: function() {

if(this.data.page < this.data.total){
  this.setData({ hidden: false });
  this.data.page = this.data.page + 1;

  this.refreshBulletins();
  
  setTimeout(callback(),5000)
  
}

},

<loading hidden="{{hidden}}">加载中...</loading>
在callback结束时
this.setData({ hidden: false});

阅读 16.7k
2 个回答

忘了写 function

可以,内部使用时,需要改变 this的指向

setTimeout((function callback(){
    this.setData({ hidden: false});
}).bind(this),5000);

模仿微信小程序,写了一个完整的例子:

var  wxAppDemo = {
    setData : function( data ){
        //模仿 微信小程序的 setData 方法
        console.log(data);
    },
    start : function(callback){
        callback = callback || function(){
            this.setData({ Error: 'callback is not a function'});
        };
        setTimeout( callback.bind(this),100);    //等5秒太慢了
    }
};

wxAppDemo.start();

wxAppDemo.start(function(){
    this.setData({ hidden: false});
});

可以试试这样写

setTimeout(function(){
    this.setData({
    hidden: false
    })
}.bind(this),5000)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题