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

网站怎么做网站收录青岛百度代理公司

网站怎么做网站收录,青岛百度代理公司,WordPress移植emlog,莱芜雪野湖介绍由于微信小程序与普通网页的开发、编译、运行机制都有所不同,在防抖节流的方法使用上也就需要我们做一些比较棘手的适配操作。常见的H5开发的防抖节流此处就不再分享了,网上有太多的教程,或者直接问那群AI即可。 OK,言归正传&…

由于微信小程序与普通网页的开发、编译、运行机制都有所不同,在防抖节流的方法使用上也就需要我们做一些比较棘手的适配操作。常见的H5开发的防抖节流此处就不再分享了,网上有太多的教程,或者直接问那群AI即可。

OK,言归正传,直接上代码:

一、防抖函数(TS版)及其使用案例

1、文件:pages/utils/index.ts

/**  * 防抖函数 debounce*  * @param fn 要防抖的函数* @param wait 等待时间,默认为500毫秒* @param isImmediate 是否立即执行,默认为true* @returns 返回防抖处理后的函数*/
let timerId: number | null = null;
let flag = true;
export const debounce = (fn: { apply: (arg0: any, arg1: IArguments) => void; }, wait = 500, isImmediate = true) => {if (isImmediate) {return function () {// @ts-ignoreconst context = this;timerId && clearTimeout(timerId);if (flag) {fn.apply(context, arguments);flag = false;}timerId = setTimeout(() => {flag = true;}, wait);};}return function () {// @ts-ignore 将当前上下文(this)赋值给 context  const context = this;timerId && clearTimeout(timerId);timerId = setTimeout(() => {fn.apply(context, arguments);}, wait);};
};

2、文件:pages/views/demoPage/index.ts

import { debounce } from '../../utils/index';Page({/** 实际需要执行的方法 */clickHandler(e: any) {console.log('[clickHandler] e.currentTarget.dataset >>>');console.log(e.currentTarget.dataset);},/*** 防抖处理后的方法* 说明一下:其实这个才是重点,debounce写完以后,怎么绑定到页面中是个大问题!* 再尝试多次后,得出以下正确使用方式!* 当然,如果有更好的写法,欢迎大家评论补充,感谢一起分享!*/dbClickHandler(e) {debounce(this.clickHandler)(e);},
});

3、文件:pages/views/demoPage/index.wxml

这个就正常的bind就行,需要稍微注意的就是要bind包裹了一层防抖函数的 dbClickHandler,如下:

<button data-key="demoKey" bind:tap="dbClickHandler"></button>

二、节流函数(TS版)及其使用案例

节流的场景相对防抖还是比较少的,但是例如搜索框的大舌头效果还是很经典的,这个搜索提示的场景要求我们在节流的同时,必须确保用户最后一次输入的值执行搜索函数,因此该方法还是有那么点麻烦。

1、文件:pages/utils/index.ts

// 定义一个泛型类型 Func,代表任何接收任意参数并返回任意结果的函数  
type Func = (...args: any[]) => any;
/**  * 节流函数,用于限制给定函数的执行频率。  *  * @param fn 需要进行节流的函数。  * @param delay 函数执行之间的延迟时间,以毫秒为单位。默认为500毫秒。  * @returns 返回一个新函数,当调用该新函数时,会根据指定的延迟时间执行被节流的函数。  */
export function throttle(fn: Func, delay = 500) {let lastFunc: any;let lastRan: number;let context: any;let args: any;let result: any;// 定义一个内部函数 executeFunc,用于实际执行传入的函数 fn const executeFunc = function () {result = fn.apply(context, args);lastRan = Date.now();clearTimeout(lastFunc as any);lastFunc = null;context = null;args = null;};// 返回一个新的函数,该函数在被调用时会执行节流逻辑  return function () {// @ts-ignore 将当前上下文(this)赋值给 self  const self = this;context = self;args = arguments;const now = Date.now();if (!lastRan) {executeFunc();lastRan = now;} else if (now - lastRan < delay) {if (lastFunc) {clearTimeout(lastFunc as any);}lastFunc = setTimeout(executeFunc, delay);} else {executeFunc();}return result;};
}

2、文件:pages/views/demoPage/index.ts

import { throttle } from '../../utils/index';Page({/** 实际需要执行的方法 */myInputChange(e: any) {console.log('[myInputChange] doing...');console.log('[myInputChange] id:', e.target.id);console.log('[myInputChange] value:', e.detail.value);},/*** 节流处理后的方法* 注意:为了能成功用上节流这个方法,此处的写法也必须是这样类似选项式的写法*/throttleInputChange: throttle(function(e) {console.log('[throttleInputChange] doing...');// @ts-ignorethis.myInputChange(e)}),
});

3、文件:pages/views/demoPage/index.wxml

以下是一个地址搜索框的例子,还是注意下bind包裹后的节流函数throttleInputChange即可。

<input id='address' placeholder="小区/写字楼/学校等" bindinput="throttleInputChange" />

END.

http://www.mmbaike.com/news/45651.html

相关文章:

  • 如何利用国外网站做自媒体青岛运营网络推广业务
  • 医院网站案例精准营销包括哪几个方面
  • 龙采哈尔滨建站公司企业网络营销策略案例
  • 临沂做网站系统百度游戏中心
  • 大连的网站设计公司电话南京网站设计
  • 免费做外贸的网站平台有哪些seo研究中心vip课程
  • 深圳市政府网站建设公司中国免费网站服务器下载
  • 福田网站建设运营费用广告公司推广文案
  • 先申请网站空间网络站点推广的方法
  • 高端装修公司怎么获客关键词优化公司哪家推广
  • 网站上线发布流程常州百度推广代理公司
  • 安徽政府网站建设semiconductor
  • 中国十大小说网站排名免费发布广告信息的网站
  • 网站建设与管理 ppt模板服务器租用
  • 沈阳市建设监理协会网站优化seo设置
  • 山西网站建设报价单流量平台排名
  • seo网站设计就业前景河南seo关键词排名优化
  • 湛江免费企业建站免费网站提交入口
  • 成都网站google翻译
  • 杭州企业网站制作哪个好去了外包简历就毁了吗
  • 仿win8 html5微网站纯手工代码营销型网站建设方案
  • 深圳网站建设大公司搜外网 seo教程
  • 护肤品网站建设环境分析百度快照怎么弄
  • 做照片视频的网站搜索引擎关键词的工具
  • 做网站新科网站建设企业网站seo托管怎么做
  • 网站建设项目方案女生学市场营销好吗
  • 财务软件排名北京网站优化步
  • 改革开放40周年网站发展建设推广一款app的营销方案
  • 南通专业制作网站百度问一问免费咨询
  • 邢台做网站哪个网络公司好火狐搜索引擎