1. // toast.js
    2. class Toast {
    3. construtor(code) {
    4. if (code !== 1000) {
    5. throw('请调用toast.init()生成实例')
    6. }
    7. this.timer
    8. this.instance
    9. this.create()
    10. }
    11. create() {
    12. this.ele = document.createElement('div')
    13. this.ele.classList.add('toast')
    14. }
    15. show(str, duration) {
    16. // 展示
    17. if(timer) {
    18. this.hide()
    19. }
    20. this.ele.innerHTML = str
    21. this.ele.classList.add('show')
    22. this.timer = setTimeOut(()=> {
    23. this.hide()
    24. })
    25. }
    26. hide() {
    27. // 隐藏
    28. this.ele.innerHTML = ''
    29. this.ele.classList.remove('show')
    30. clearTimeout(this.timer)
    31. }
    32. static init() {
    33. return this.instance || (this.instance = new Toast(1000))
    34. }
    35. }