当前位置: 首页 > news >正文

微信小程序app.js中每30秒调用一次wx.getLocation

代码:

const auth = require('./utils/auth.js');  // 引入 auth.js
// app.js
App({onLaunch() {// 原有登录检查逻辑const isLoggedIn = auth.checkLogin();if (!isLoggedIn) {wx.removeStorageSync('token');wx.removeStorageSync('userInfo');wx.reLaunch({ url: '/pages/login/login' });    }// 新增定时定位逻辑this.startLocationInterval();},startLocationInterval() {// 立即执行一次this.getAndCacheLocation();// 设置30秒定时器this.locationTimer = setInterval(() => {this.getAndCacheLocation();}, 30000);},getAndCacheLocation() {wx.getLocation({type: 'wgs84',success: (res) => {console.log("lat="+res.latitude+'&lon='+res.longitude)wx.setStorageSync('lastLocation', {latitude: res.latitude,longitude: res.longitude,timestamp: new Date().getTime()});},fail: (err) => {console.error('定位失败', err);}});},onHide() {// 小程序进入后台时清除定时器if (this.locationTimer) {clearInterval(this.locationTimer);}},onUnload() {// 双重保险if (this.locationTimer) {clearInterval(this.locationTimer);this.locationTimer = null;}},onShow() {// 小程序回到前台时重启定时器this.startLocationInterval();}
})

最后注意清除定时器:

http://www.dtcms.com/a/423373.html

相关文章:

  • 遥感云平台-GEE分块下载与拼接
  • 深圳市建设监理协会网站wordpress后台接口数据
  • UNIX下C语言编程与实践10-UNIX 动态库隐式调用:编译链接配置与路径问题排查
  • 网站虚拟主持人制作建设一个网站需要条件
  • 网站建设中文百北京住房和城乡建设局门户网站
  • uni-app 模板语法修复说明
  • OpenLayers地图交互 -- 章节十八:拖拽旋转和缩放交互详解
  • 6DOF-Euler Angles
  • 【代码随想录day 29】 力扣 860.柠檬水找零
  • 医疗智能体的技术演进与路径分析:从多模态大模型到高阶综合智能体
  • 西安制作网站的电话深圳seo网站推广方案
  • 开放、协同,2025 云栖大会“操作系统开源与 AI 进化分论坛”精彩回顾
  • Codeforces Round 1050 A. Sublime Sequence
  • 欧拉路径与欧拉回路
  • 【C语言】计算两个整数二进制表示中不同位的个数
  • T5相对位置编码
  • 网站模板分类济阳做网站多少钱
  • 怎样做网站反链绵阳网站
  • Excel转PDF不分页
  • Serverless架构:无服务器计算的全面解析与实践指南
  • 记一次编译 debug 版本的 python 3.12.11 的过程
  • 需要上传视频的网站什么是html5网站
  • 深入Spring Boot的核心——配置管理(指南四)
  • 打工人日报#20250929
  • 论 AI Database
  • 免费建设网站公司哪家好如何做公司培训网站
  • 美工网站设计网站网页转小程序教程
  • 【JVM】基础篇(一)
  • 【关于虚拟机执行ip addr 命令不显示ip地址问题】
  • SpringBoot快速生成二维码