Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>默认</h2>
<button class="btn1">添加一条数据</button>
<button class="btn2">输出这条数据</button>
<button class="btn3">删除这条数据</button>
<br/>
<br/>
<h2>自定义2秒失效</h2>
<button class="btn4">添加一条2秒过期的数据</button>
<button class="btn5">alert提示这个2秒数据</button>
              
            
!

CSS

              
                
              
            
!

JS

              
                //配置类型
interface AutoDestructionOptions {
  expiryTime: number; //过期时间ms
}
//自动销毁类
class AutoDestruction {
  private _expiryTime: number; //默认过期时间
  private _map = new Map(); //存储的数据对象

  constructor(options: AutoDestructionOptions) {
    const { expiryTime } = options;

    this._expiryTime = expiryTime;
  }

  /**
   * @description: get方法
   * @param {any} key
   * @Date: 2022-04-15 00:47:52
   * @Author: mulingyuer
   */
  public get(key: any): any {
    //自动删除过期数据
    this.autoDelete(key);
    //再拿结果
    const results = this._map.get(key);

    return results ? results.value : results;
  }

  /**
   * @description: set方法
   * @param {any} key
   * @param {any} val
   * @Date: 2022-04-15 00:48:03
   * @Author: mulingyuer
   */
  public set(key: any, val: any, time?: number): AutoDestruction {
    let expiryTime = this._expiryTime;
    if (typeof time === "number") {
      expiryTime = time;
    }
    //保存数据
    this._map.set(key, {
      saveTime: Date.now(), //存储时的时间
      expiryTime, //多久过期
      value: val, //存储的值
    });

    //抛出自己
    return this;
  }

  /**
   * @description: has方法
   * @param {any} key
   * @Date: 2022-04-15 00:51:48
   * @Author: mulingyuer
   */
  public has(key: any): boolean {
    //自动删除过期数据
    this.autoDelete(key);

    return this._map.has(key);
  }

  //size属性
  public get size(): number {
    //自动删除过期数据:计算size那只能先删一波
    this.autoDelete();

    return this._map.size;
  }

  /**
   * @description: delete方法
   * @param {any} key
   * @Date: 2022-04-15 00:53:43
   * @Author: mulingyuer
   */
  public delete(key: any): boolean {
    return this._map.delete(key);
  }

  /**
   * @description: 自动删除
   * @param {any} key
   * @Date: 2022-04-15 00:12:17
   * @Author: mulingyuer
   */
  private autoDelete(key?: any): void {
    if (typeof key === "string") {
      const value = this._map.get(key);
      if (!value) return;
      const { saveTime, expiryTime } = value;
      if (this.isExpiry(saveTime, expiryTime)) {
        this._map.delete(key);
      }
    } else {
      const expiryKeys: Array<any> = []; //过期key
      this._map.forEach((val, key) => {
        const { saveTime, expiryTime } = val;
        //超过有效期记录
        if (this.isExpiry(saveTime, expiryTime)) expiryKeys.push(key);
      });
      expiryKeys.forEach((key) => this._map.delete(key));
    }
  }

  /**
   * @description: 是否已过期
   * @param {number} expiryTime
   * @Date: 2022-04-15 00:21:43
   * @Author: mulingyuer
   */
  private isExpiry(saveTime: number, expiryTime: number): boolean {
    return Date.now() > saveTime + expiryTime;
  }
}

//业务测试代码
const autoDestruction = new AutoDestruction({ expiryTime: 10000 });  //默认10秒过期
function ready(fn) {
  if (document.readyState != 'loading'){
    fn();
  } else {
    document.addEventListener('DOMContentLoaded', fn);
  }
}

ready(function(){
  function query(selector) {
    return document.querySelector(selector);
  };
  const addClick = (dom,fn)=>dom.addEventListener("click",fn);

  const btn1 = query(".btn1");
  const btn2 = query(".btn2");
  const btn3 = query(".btn3");
  const btn4 = query(".btn4");
  const btn5 = query(".btn5");

  addClick(btn1,()=>{
    autoDestruction.set("测试1","我是一条测试数据,有效期默认10s");
    alert("添加成功")
  });
  
  addClick(btn2,()=>{
    const val = autoDestruction.get("测试1");
    const str = typeof val !== "string" ? typeof val : val;
    alert(str);
  });
  
  addClick(btn3,()=>{
    alert(autoDestruction.delete("测试1"))
  });
  
  addClick(btn4,()=>{
    alert(autoDestruction.set("测试2","我是一条2s过期的数据",2000))
  });

  addClick(btn5,()=>{
    alert(autoDestruction.get("测试2"))
  });
  
});

              
            
!
999px

Console