可以是通过函数包裹的方式:
以下代码演示 当页面多个条目需要触发点击事件 将当前dom 文本修改为可以看见,1s后恢复原来状态时。
$(".showOrHide").on("click", function () {
$that = $(this);
const isShow = $that.data("show");
if(isShow)return;
$that.data("show",'true')
$that.html('可以看见');
setTimeout(entrust($that), 1000)
});
//使用一个函数对处理的函数进行包裹。
function entrust($that) {
return function () {
hideTextOfTimeOut($that);
}
}
function hideTextOfTimeOut($that) {
$that.html('不可见');
$that.data('show', 'false');
}