Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

微信小程序可以正常使用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});


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

忘了写 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});
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...