欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解微信小程序的不同函数调用的几种方法

程序员文章站 2023-11-18 12:42:10
一、调取参数 直接调取当前js中的方法, 调取参数that.bindviewtap(); 二、跳转页面 navigateto: function () {...

一、调取参数

直接调取当前js中的方法,

调取参数that.bindviewtap();

二、跳转页面

navigateto: function () {
wx.navigateto({ url: '../page4/page4' });
},
 
 
全局变量使用方法
a.js
var app = getapp()
page({
data: {
hex1: [],
})}
//设置全局变量
if (hex1 != null) {
app.globaldata.hex1 = hex1;
}
 
b.js
接收全局变量
var app = getapp()
page({
 data:{
  hex1:[]
 },
 
onload:function(options){
   this.setdata({
   hex1:getapp().globaldata.hex1
  });
 },
 
})

三、获取事件的方法:

获取事件的方法:
bindviewtap1: function (e) {
console.log('事件event',e)
}
 
监听input方法
a.wxml页面
<input type="number" maxlength="2" placeholder="小于100分钟" placeholder-class="psection" bindinput="listenertimeinput" />
 
a.js页面
//监听input框输入
listenertimeinput: function (e) {
this.data.time = e.detail.value;
console.log('哒哒this.data.time', this.data.time)
},
 
获取当前时间戳
var timestamp = date.parse(new date());
timestamp = timestamp / 1000;
console.log("当前时间戳为:" + timestamp);
 
时间转换为秒
var stime = '';
var format = '';
stime = time * 60;
console.log('秒', stime);
 
转换为时间
date.prototype.format = function (format) {
var date = {
"m+": this.getmonth() +1,
"d+": this.getdate(),
"h+": this.gethours(),
"m+": this.getminutes(),
"s+": this.getseconds(),
"q+": math.floor((this.getmonth() +3) /3),
"s+": this.getmilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(regexp.$1, (this.getfullyear() +'').substr(4 - regexp.$1.length));
}
for (var kin date) {
if (new regexp("(" + k +")").test(format)) {
format = format.replace(regexp.$1, regexp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
var oktime = '';
oktime = newdate.format('yyyy-mm-dd h:m:s');
 
//获取当前时间
var date = new date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getmonth() + 1;
console.log('大大', month);
var strdate = date.getdate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
console.log('大大', month);
if (strdate >= 0 && strdate <= 9) {
strdate = "0" + strdate;
}
var currentdate = date.getfullyear() + seperator1 + month + seperator1 + strdate + " " + date.gethours() + seperator2 + date.getminutes() + seperator2 + date.getseconds();
console.log('当前时间', currentdate);
console.log('年', date.getfullyear());
console.log('月', month);
console.log('日', date.getdate());
console.log('时', date.gethours())
console.log('分', date.getminutes())
 
 
转为串
oktime = oktime.tostring();
 
提取数值
var oktime = oktime.replace(/[^0-9]/ig, "");
console.log('oktime', oktime);
 
获取年 月 日 时 分 秒
var year1 = '';var month1 ='';var date1 ='';var hours1 ='';var min1 ='';
year1 = oktime.substring(2, 4);
console.log('year1', year1);
month1 = oktime.substring(4, 6);
console.log('month1', month1);
date1 = oktime.substring(6, 8);
console.log('date1', date1);
hours1 = oktime.substring(8, 10);
console.log('hours1', hours1);
min1 = oktime.substring(10, 12);
console.log('min1', min1);
 
转换为16进制
  
var zhen4000 = parseint(zhen400);
  
console.log('飞zhen4000', zhen4000)
    
zhen4000 = zhen4000.tostring(16);
 
console.log('@@@@@zhen4000', zhen4000);
 
将值由16进制转10进制
zhen1 = parseint(zhen001, 16);
 
// 进行异或运算
//注:我使用过16进制的异或运算,没成功,最后使用了比较复杂的方法,由十六进制转化为十进制异或运算,将得到的值在转为十六进制
zhen9 = zhen1 ^ zhen2

四、点击事件url传值

点击事件url传值
wx.redirectto({
url: '../page1/page1?deviceid='+title+'&name='+name,
success: function(res){
// success
},
 
.js传值到.wxml页面
js页面
this.setdata({
ntc: ntc,
result: ntc
})
wxml页面
<rich-text class='tep'>{{ntc}}℃</rich-text>
 
.wxml页面bindtap点击事件传值到.js 页面跳转 监听id传值
bindtap="bindviewtap1"值触发到到js里面的bindviewtap1:function方法
.wxml
<navigatorbindtap="bindviewtap1"id='time1'url="../page5/page5"hover-class="navigator-hover">
 
.js
bindviewtap1: function (e) {
//hex1
var id = e.currenttarget.id;
console.log('坎坎坷坷扩id',id);
}
 
设置跳出循环
//跳出循环,只运行一次
在data()里面添加
flag : '',
在你使用的方法里面加
var flag = true;
if (flag) {
that.bindviewtap3();
that.bindviewtap2();
flag = false;
}
 
 
函数间传值
//设置全局data
 data{rtime:''}
 
btn1: function(e){
 this.setdata({
   rtime : time
  })
}
 
btn2:funciton(e){
 console.log('###@@@@@#',this.data.rtime)
} 

以上所述是小编给大家介绍的微信小程序的不同函数调用的几种方法详解整合,希望对大家有所帮助