對(duì)于JS編程高手可略過此文,僅供新手學(xué)習(xí)參考。
創(chuàng)新互聯(lián)主營(yíng)政和網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),政和h5小程序開發(fā)搭建,政和網(wǎng)站營(yíng)銷推廣歡迎政和等地區(qū)企業(yè)咨詢
打開微信小程序,隨便創(chuàng)建一個(gè)頁面(不妨設(shè)置為test)。然后,在test.js文件中加入如下代碼:
data: {
times: 0
},
/**
生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
var that = this;
var term = setInterval(function () {
console.log('times:',that.data.times);
if (that.data.times ===3) {
clearInterval(term);
console.log('END...');
}else{
that.setData({ times: ++that.data.times})
}
}, 1000);
},
如果把上述代碼完全替換為如下:
data: {
times: 0
},
/**
生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
var that = this;
var term = setInterval(function () {
console.log('times:',that.data.times);
if (that.data.times ===3) {
clearInterval(term);
console.log('END...');
}else{
that.setData({ times: that.data.times++})
}
}, 1000);
},