锁定计算机代码,小程序里面的锁的代码

针对小程序的异步

因为懒得去配置async 和await的环境了,wx.getStorageSync是同步的就

直接了在这个基础上了一个重入锁的js来实现同步

/***本类主要用来实现同步*/

const lockerKey = "wxlocker";

var lockerContainer={};

/**

* 加锁函数

* 参数:锁名

*

*/

function lock(lockName){

var obj= wx.getStorageSync(lockerKey);

console.log(111);

console.log(obj);

if (typeof obj === "undefined" || obj==null||obj==''){

lockerContainer.lockName = 1;

wx.setStorageSync(lockerKey, lockerContainer);

}else{

lockerContainer = obj;

if (typeof lockerContainer.lockName !== "undefined" && lockerContainer.lockName != null && lockerContainer.lockName != ''){

lockerContainer.lockName = lockerContainer.lockName +1;

}else{

lockerContainer.lockName = 1;

}

//更新锁

wx.setStorageSync(lockerKey, lockerContainer);

}

}

/**

* 解锁函数

* 参数:锁名

*

*/

function unlock(lockName){

var obj = wx.getStorageSync(lockerKey);

console.log(222);

console.log(obj);

if (typeof obj === "undefined" || obj == null || obj == '') {//没有锁过

return;

} else {

lockerContainer = obj;

if (typeof lockerContainer.lockName !== "undefined" && lockerContainer.lockName != null && lockerContainer.lockName != '') {

if (lockerContainer.lockName <=0){

lockerContainer.lockName=0;

return;

}else{

lockerContainer.lockName = lockerContainer.lockName - 1;

}

} else {

return;

}

//更新锁

wx.setStorageSync(lockerKey, lockerContainer);

obj = wx.getStorageSync(lockerKey);

console.log(obj);

}

}

/**

* 是否被锁函数

* 参数:锁名

* 返回值:boolean

*/

function isLockedBy(lockName){

var obj = wx.getStorageSync(lockerKey);

if (typeof obj === "undefined" || obj == null || obj == '') {//没有锁过

return false;

} else {

lockerContainer = obj;

if (typeof lockerContainer.lockName !== "undefined" && lockerContainer.lockName != null && lockerContainer.lockName != '') {

if (lockerContainer.lockName > 0) {

return true;

} else {

return false;

}

} else {

return false;

}

}

}

/**

* 等待函数(等待100*millseconds后自动解除)

* 参数:锁名,间隔毫秒,等待解锁完成后的回调函数

*

*/

function wait(lockName, millseconds,callback){

//100次监控后清空锁

var expireTime = 0;

var timer =setInterval(function () {

console.log("wait " + millseconds);

if (!isLockedBy(lockName)){

clearInterval(timer);

callback();

}else{//被锁住了

expireTime++;

if (expireTime > 100) {

lockerContainer.lockName=null;

//清空锁

wx.setStorageSync(lockerKey, lockerContainer);

clearInterval(timer);

}

}

}, millseconds)

}

module.exports = {

wait: wait,

unlock: unlock,

lock: lock

}

用法,加锁多少次就要解锁多少次

lock("mylock")

lock("mylock")

unlock("mylock")

unlock("mylock")

wait("mylock",1000,callback)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小程序地图可查看范围的经纬度代码需要在地图组件中进行设置。具体步骤如下: 1. 在 wxml 文件中添加地图组件代码: ``` <map id="myMap" latitude="{{latitude}}" longitude="{{longitude}}" scale="{{scale}}" markers="{{markers}}" show-location="{{showLocation}}" bindregionchange="regionchange" style="width: 100%; height: 100%;"></map> ``` 2. 在 js 文件中设置地图的经纬度范围: ``` Page({ data: { latitude: 23.099994, longitude: 113.324520, scale: 14, markers: [{ id: 1, latitude: 23.099994, longitude: 113.324520, name: 'T.I.T 创意园' }], showLocation: true }, regionchange(e) { console.log(e.type) }, }) ``` 在上面的代码中,latitude 和 longitude 分别表示地图的中心经纬度,scale 表示地图的缩放级别,markers 表示地图上的标记点,showLocation 表示是否显示当前位置。 如果需要锁定地图的可查看范围,可以通过 regionchange 事件的回调函数来实现。在回调函数中,可以通过 e.causedBy 属性来判断地图的操作类型。如果是 drag 操作,就可以通过调用 mapCtx.getRegion 方法来获取地图的可查看范围,并对其进行限制。 下面是具体的实现代码: ``` Page({ data: { latitude: 23.099994, longitude: 113.324520, scale: 14, markers: [{ id: 1, latitude: 23.099994, longitude: 113.324520, name: 'T.I.T 创意园' }], showLocation: true }, regionchange(e) { if (e.type === 'end' && e.causedBy === 'drag') { const mapCtx = wx.createMapContext('myMap') mapCtx.getRegion({ success: res => { const { southwest, northeast } = res const { latitude, longitude } = this.data const diffLat = northeast.latitude - southwest.latitude const diffLng = northeast.longitude - southwest.longitude const newLat = latitude < southwest.latitude ? southwest.latitude : latitude > northeast.latitude ? northeast.latitude : latitude const newLng = longitude < southwest.longitude ? southwest.longitude : longitude > northeast.longitude ? northeast.longitude : longitude const newMarkers = this.data.markers.map(marker => { const { latitude, longitude } = marker const newMarkerLat = latitude < southwest.latitude ? southwest.latitude : latitude > northeast.latitude ? northeast.latitude : latitude const newMarkerLng = longitude < southwest.longitude ? southwest.longitude : longitude > northeast.longitude ? northeast.longitude : longitude return { ...marker, latitude: newMarkerLat, longitude: newMarkerLng } }) this.setData({ latitude: newLat, longitude: newLng, markers: newMarkers }) } }) } }, }) ``` 在上面的代码中,我们首先通过 e.causedBy 判断地图操作类型是否为 drag,如果是则调用 mapCtx.getRegion 方法获取地图的可查看范围。然后根据地图的中心经纬度和可查看范围,计算出新的中心经纬度和标记点的位置,并更新 data 中的数据。最后在 wxml 文件中绑定 regionchange 事件即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值