小程序开发细节

一、文本,注意字数过多情况
有些介绍性的文本,由于不知道对其的输入是否有控制,经常会出现文本的字数过多,超出控件的显示范围,
从而使得显示与预计的不符。所以对于文本,一定要记得控制控件的高度和文本的显示行数,具体操作参考
之前的博客《微信小程序text文本溢出单行/多行省略》。
二、防连触
用户在操作小程序的时候,难免会不小心连续点两次屏幕。大多数时候这种情况出现不会有影响,但是有的时候
也会发生意想不到的结果导致程序崩溃或者错误。所以应当避免发生连触时,对系统的影响。

data:{
	tapTime='',
	},
  collectAlbum: function() {
    var nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    //收藏操作
    this.data.tapTime = nowTime;
  },

三、加载太长时给提示

    wx.showLoading({
      title: '加载中',
    })
    //加载操作
    wx.hideLoading()

四、网络超时处理
使用Promise的race方法。

//请求某个图片资源
function requestImg(){
    var p = new Promise(function(resolve, reject){
        var img = new Image();
        img.onload = function(){
            resolve(img);
        }
        img.src = 'xxxxxx';
    });
    return p;
}

//延时函数,用于给请求计时
function timeout(){
    var p = new Promise(function(resolve, reject){
        setTimeout(function(){
            reject('图片请求超时');
        }, 5000);
    });
    return p;
}

Promise
.race([requestImg(), timeout()])
.then(function(results){
    console.log(results);
})
.catch(function(reason){
    console.log(reason);
});

猜你喜欢

转载自blog.csdn.net/qq_22038259/article/details/84571333