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

一个网站建设的流程百度网站安全检测

一个网站建设的流程,百度网站安全检测,wordpress文字环绕广告,商城模板全套一、请求失败会弹出 一个toast , 如何保证批量请求失败 , 只弹出一个toast 方法一:使用全局标志位 JavaScript let isToastShown false; // 全局标志位function makeRequests() {const requests [fetchPost(),fetchComments()]; // 多个请求Promise.all(reque…

一、请求失败会弹出 一个toast , 如何保证批量请求失败 , 只弹出一个toast 

方法一:使用全局标志位

JavaScript

let isToastShown = false; // 全局标志位function makeRequests() {const requests = [fetchPost(),fetchComments()]; // 多个请求Promise.all(requests).then(() => {// 所有请求成功的处理逻辑}).catch(errors => {if (!isToastShown) {// 检查标志位notify(errors[0]); // 弹出 toastisToastShown = true; // 设置标志位为 true}});
}function fetchJSON(url, input) {return fetch(url, input).then(res => {if (res.ok) {return res.json();}const err = new HttpError(res);if (!isToastShown) { // 检查标志位notify(err); // 弹出 toastisToastShown = true; // 设置标志位为 true}throw err; // 确保错误被抛出,否则后续逻辑可能无法捕获});
}// 示例错误类
class HttpError extends Error {constructor(response) {super(`${response.status} ${response.statusText}`);this.name = "HttpError";this.response = response;}
}// 重置标志位(在新的请求开始前)
makeRequests().finally(() => {isToastShown = false; // 确保在下一次请求时标志位被重置
});
方法二:使用防抖函数

JavaScript

// 防抖函数
function debounce(fn, delay = 1000) {let timeout;return function (...args) {clearTimeout(timeout);timeout = setTimeout(() => {fn.apply(this, args);}, delay);};
}const notifyDebounced = debounce(message => {notify(message);
}, 3000); // 3 秒内只执行一次// 示例调用
fetchJSON(url, input).catch(err => {notifyDebounced(err); // 使用防抖后的通知函数});
方法三:集中处理错误

JavaScript

function makeRequests() {const requests = [fetchPost(),fetchComments()]; // 多个请求Promise.allSettled(requests).then(results => {const hasError = results.some(result => result.status === "rejected");if (hasError) {const firstError = results.find(result => result.status === "rejected").reason;notify(firstError); // 合并所有错误并只弹出一次} else {// 所有请求成功的处理逻辑}});
}

二、减少项目中的 if-else 语句

1. 策略模式

JavaScript

// 定义策略对象
const strategies = {condition1: (arg) => {// 处理条件 1 的逻辑console.log(`Handling condition1 with argument: ${arg}`);},condition2: (arg) => {// 处理条件 2 的逻辑console.log(`Handling condition2 with argument: ${arg}`);},// 更多条件和处理逻辑
};// 动态获取条件并执行策略
function handleCondition(condition, arg) {const strategy = strategies[condition];if (strategy) {strategy(arg);} else {console.log(`Unknown condition: ${condition}`);}
}// 示例调用
handleCondition("condition1", "foo"); // 输出:Handling condition1 with argument: foo
handleCondition("condition3", "bar"); // 输出:Unknown condition: condition3
2. 表驱动法(动态条件)

JavaScript

const handlers = {"condition1": (arg) => console.log(`Handling condition1 with ${arg}`),"condition2": (arg) => console.log(`Handling condition2 with ${arg}`),// 其他条件处理函数
};function processConditions(conditions, arg) {conditions.forEach(condition => {const handler = handlers[condition];handler && handler(arg);});
}// 示例调用
processConditions(["condition1", "condition2"], "data"); // 输出两个条件的处理结果
3. 提取函数

JavaScript

function handleCaseA() {// 大量逻辑 A
}function handleCaseB() {// 大量逻辑 B
}function main() {const type = getType();if (type === "A") {handleCaseA();} else if (type === "B") {handleCaseB();} else {console.log("Unknown type");}
}
4. 状态模式

JavaScript

// 定义状态类
class State {handle(context) {console.log("Default state handling");}
}class StateA extends State {handle(context) {console.log("Handling State A");context.state = new StateB(); // 转换状态}
}class StateB extends State {handle(context) {console.log("Handling State B");context.state = new StateA();}
}// 上下文对象
class Context {constructor() {this.state = new StateA();}request() {this.state.handle(this);}
}// 示例调用
const context = new Context();
context.request(); // 输出:Handling State A
context.request(); // 输出:Handling State B

总结

  • 全局标志位:适用于简单的单次通知场景。

  • 防抖节流:适用于需要限制通知频率的场景。

  • 集中处理错误:适用于批量请求的错误统一管理。

  • 策略模式/表驱动法:将条件逻辑抽离为独立模块,增强可维护性。

  • 状态模式:适用于对象状态频繁变化的场景。

http://www.dtcms.com/wzjs/135247.html

相关文章:

  • wordpress关闭发表评论百度seo排名优化软件分类
  • 电子商务公司开发网站成都网站建设技术支持
  • 北京建设协会网站网络营销的效果是什么
  • 河南网站建设价格大全网页设计制作网站图片
  • 深圳做网站最好自己做网站需要什么条件
  • 网站创建时间查询站长之家seo查询官方网站
  • 网站开发进入腾信职位seo搜索引擎
  • 网站后台流程广州网页定制多少钱
  • 番禺高端网站建设公司网站优化排名服务
  • 移动端社区 wordpress广州网站优化服务商
  • 阿里云做的网站程序员软文宣传推广
  • 要做网站最近的新闻事件
  • 2003系统做网站百度app优化
  • 免费的高清视频素材网站seo排名外包
  • wordpress建站系统视频教程上海谷歌优化
  • 电信网站开发语言主要用什么百度seo关键词优化工具
  • 廊坊网站建设佛山厂商在线生成个人网站app
  • 有个可以做图片的网站优化大师官网入口
  • 做直销建立个人网站好吗北京优化seo排名优化
  • adobe photoshopseo外链优化
  • 网站开发编译器百度客服工作内容
  • 涞源网站建设成人技术培训学校
  • 大学专业建设的内容北京seo外包平台
  • 做全国社保代理的网站百度助手免费下载
  • 惠州设计公司logoseo网站推广教程
  • 做微商在哪个网站打广告好合肥做网络推广的公司
  • 建一个企业网站要花多少钱市场调研报告word模板
  • b站推广网站2023年绍兴seo公司
  • 东营 网站建设谷歌chrome
  • 网站建设培训南宁汕头网站建设