【问题标题】:Is there Any JavaScript method to stop a function from working是否有任何 JavaScript 方法可以阻止函数工作
【发布时间】:2018-06-09 15:33:00
【问题描述】:

我有一个函数,它有一个 if 语句和一个 onclick 函数。我希望当 if 语句为真时,整个函数不应该工作。 我试图做 return false 并且它对我不起作用

showLogo: function(row) {
  var a = 0;
  window.onclick = (e => {
    let element = e.target.className || e.srcElement.className
    let element1 = e.target || e.srcElement
    if (element1.localName == "li" || element == "pagination-previous" || element == "pagination-next" || element === "pagination-link" || element === "icon" || element === "fa fa-angle-right fa-lg" || element === "b-table" || element === "level" || element === "pagination" || element === "fa fa-angle-left fa-lg") {
      this.logo = false
    }
  });
  if (window.innerWidth > 1024) {
    this.logo = true;
    this.color = false;
    this.istabModalActive = false;
    this.name = row.fileName;
    this.dimension = row.width + ' x ' + row.height
    this.width = 0;
    this.height = 0;
    this.src = row.path;
    this.icon = row.iconsrc;
    this.dlsrc = row.path;
    this.contentType = row.contentType;
    this.typeshow = row.contentType.split("/")[1].toUpperCase();
    this.type = row.contentType.split("/")[1];
    this.icons = 'dist/' + this.type + '.svg';
    this.assetsid = row.index;
    this.size = row.size;
    this.activerow = row;
    this.fileid = row.id;
    this.assetsid = row.id;
    this.activerow = row;
  } else {
    this.logo = true;
    this.color = false;
    this.istabModalActive = true;
    this.filesarr = row;
    this.name = row.fileName;
    this.dimension = row.width + ' x ' + row.height
    this.size = row.size;
    this.src = row.path;
    this.icon = row.iconsrc;
    this.dlsrc = row.path;
    this.typeshow = row.contentType.split("/")[1].toUpperCase();
    this.type = row.contentType.split("/")[1];
    this.icons = row.iconsrc;
    this.fileid = row.id;
    this.assetsid = row.id;
    this.activerow = row;
  }

}

【问题讨论】:

  • Early exit from function?的可能重复
  • 我正在使用 vue.js 和 Ecmascript
  • 使用return 来阻止它
  • 我已经编辑了我的帖子,请检查我正在使用的完整功能,我使用了 return 但它不起作用
  • @HaroonAslam 但是,当您从中 return 时,该功能将不会进一步执行。这不是你想要的吗?

标签: javascript ecmascript-6 vue.js frontend


【解决方案1】:
setWindowOnCLick : function() {
  window.onclick = (e => {
        let element = e.target.className || e.srcElement.className
        let element1 = e.target || e.srcElement
        if (element1.localName == "li" || element == "pagination-previous" || element == "pagination-next" || element === "pagination-link" || element === "icon" || element === "fa fa-angle-right fa-lg" || element === "b-table" || element === "level" || element === "pagination" || element === "fa fa-angle-left fa-lg") {
          this.logo = false
        }
      });
}  
showLogo: function(row) {
      var a = 0;

      if (window.innerWidth > 1024) {
        this.logo = true;
        this.color = false;
        this.istabModalActive = false;
        this.name = row.fileName;
        this.dimension = row.width + ' x ' + row.height
        this.width = 0;
        this.height = 0;
        this.src = row.path;
        this.icon = row.iconsrc;
        this.dlsrc = row.path;
        this.contentType = row.contentType;
        this.typeshow = row.contentType.split("/")[1].toUpperCase();
        this.type = row.contentType.split("/")[1];
        this.icons = 'dist/' + this.type + '.svg';
        this.assetsid = row.index;
        this.size = row.size;
        this.activerow = row;
        this.fileid = row.id;
        this.assetsid = row.id;
        this.activerow = row;
      } else {
        this.logo = true;
        this.setWindowOnCLick.call(this)
        //thismay solve your problem
        this.color = false;
        this.istabModalActive = true;
        this.filesarr = row;
        this.name = row.fileName;
        this.dimension = row.width + ' x ' + row.height
        this.size = row.size;
        this.src = row.path;
        this.icon = row.iconsrc;
        this.dlsrc = row.path;
        this.typeshow = row.contentType.split("/")[1].toUpperCase();
        this.type = row.contentType.split("/")[1];
        this.icons = row.iconsrc;
        this.fileid = row.id;
        this.assetsid = row.id;
        this.activerow = row;
      }

    }

【讨论】:

  • 谢谢,但没有工作我稍微调整了一下,但没有得到任何结果
【解决方案2】:
showLogo:function(row){
    var a = 0;
    var flag = 0;
    window.onclick = (e => {
        let element = e.target.className || e.srcElement.className
        let element1 = e.target || e.srcElement
        if (element1.localName == "li" || element == "pagination-previous" || element == "pagination-next" || element === "pagination-link" ||  element === "icon" ||  element === "fa fa-angle-right fa-lg" ||  element === "b-table"||  element === "level"||  element === "pagination" ||  element === "fa fa-angle-left fa-lg") {
            this.logo = false;
            flag = 1;
        }


        }); 
                if(window.innerWidth> 1024 && flag == 0)
                {
                this.logo = true;
                this.color = false; 
                this.istabModalActive = false;
                this.name = row.fileName;
                this.dimension = row.width +' x '+row.height
                this.width = 0;
                this.height = 0;
                this.src = row.path;
                this.icon = row.iconsrc;
                this.dlsrc = row.path;
                this.contentType = row.contentType;
                this.typeshow = row.contentType.split("/")[1].toUpperCase() ;
                this.type = row.contentType.split("/")[1];
                this.icons = 'dist/'+this.type+'.svg';
                this.assetsid=row.index;
                this.size = row.size;
                this.activerow = row;
                this.fileid=row.id;
                this.assetsid=row.id;
                this.activerow = row;
                }
                else if(flag == 0) {
                this.logo = true;
                this.color = false; 
                this.istabModalActive = true;
                this.filesarr=row;
                this.name = row.fileName;
                this.dimension = row.width +' x '+row.height
                this.size = row.size;
                this.src = row.path;
                this.icon = row.iconsrc;
                this.dlsrc = row.path;
                this.typeshow = row.contentType.split("/")[1].toUpperCase() ;
                this.type = row.contentType.split("/")[1];
                this.icons = row.iconsrc;
                this.fileid=row.id;
                this.assetsid=row.id;
                this.activerow = row;   
                }

},

【讨论】:

  • 你试过了吗?
  • 是的,但没有工作,因为 onclick 中的标志没有脱离window.onclick 函数,我使用警报来找出
  • 在onclick外面声明flag试试
  • 我已经按照你所说的做了一些事情,但是在第一次尝试时它不应该这样做,然后它就像我想要的那样工作
  • 并不是在第二次尝试使用 onlick 功能后窗口没有打开,它不能正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 2016-12-12
  • 1970-01-01
  • 2011-04-25
  • 2016-09-15
  • 2011-12-03
相关资源
最近更新 更多