简体   繁体   English

仅当另一个条件已经执行时才执行条件

[英]Execute a conditional only if another conditional has already been executed

I'm trying to send two events to Google Analytics when two if conditionals are met when the user resizes the browser window.当用户调整浏览器 window 的大小时满足两个if时,我正在尝试向 Google Analytics 发送两个事件。

These events are: Mobile browser width and Non-mobile browser width这些事件是:移动浏览器宽度非移动浏览器宽度

For this, I am using the following code:为此,我使用以下代码:

$(window).resize(function(){
   if(window.outerWidth >= 800){
     gtag('event', 'Mobile browser width', {'event_category': 'BrowserWidth'});
   
   if(window.outerWidth <= 801){
   gtag('event', 'Non-mobile browser width', {'event_category': 'BrowserWidth'});
   }
  });

But I am facing a problem: due to the nature of .resize () , these conditionals are met for each resized pixel, which consequently causes the gtag () event to be fired numerous times, and that is not what I expected to happen.但是我面临一个问题:由于.resize ()的性质,每个调整大小的像素都满足这些条件,从而导致gtag ()事件被触发多次,这不是我所期望的。

What I want is for the Mobile browser width event to be triggered once, and to be triggered again only if the Non-mobile browser width event has already been triggered.我想要的是移动浏览器宽度事件被触发一次,并且只有在非移动浏览器宽度事件已经被触发时才会再次被触发。

Here is an example of the order in which these events should be sent to Google Analytics:以下是应将这些事件发送到 Google Analytics 的顺序示例:

1st resize() activation: Is window.outerWidth <= 801 ?第一次resize()激活:是window.outerWidth <= 801吗? True!真的! Then dispatch Mobile browser width event然后调度移动浏览器宽度事件

2nd resize() activation: Is window.outerWidth <= 801 ?第二次resize()激活:是window.outerWidth <= 801吗? True!真的! Then do not dispatch Mobile browser width event然后不要调度移动浏览器宽度事件

3rd resize() activation: Is window.outerWidth >= 800 ?第三次resize()激活:是window.outerWidth >= 800吗? True!真的! Then dispatch Non-mobile browser width event然后调度非移动浏览器宽度事件

4th resize() activation: Is window.outerWidth <= 801 ?第 4 次resize()激活:是window.outerWidth <= 801吗? True!真的! Then dispatch Mobile browser width event然后调度移动浏览器宽度事件

5th resize() activation: Is window.outerWidth >= 800 ?第 5 次resize()激活:是window.outerWidth >= 800吗? True!真的! Then dispatch Non-mobile browser width event然后调度非移动浏览器宽度事件

... and so on. ... 等等。

This is the most didactic way I found to exemplify how I want events to be sent to Google Analytics.这是我发现的最能说明我希望如何将事件发送到 Google Analytics(分析)的方式。 Could someone help me on how to adapt my code to respect these rules?有人可以帮助我如何调整我的代码以遵守这些规则吗?

You could store a reference to the previously fired event then check that variable before firing a new event.您可以存储对先前触发的事件的引用,然后在触发新事件之前检查该变量。 Something like:就像是:

let latestEvent;
$(window).resize(function () {
  if (window.outerWidth >= 800 && latestEvent !== 'mobile') {
    gtag('event', 'Mobile browser width', { event_category: 'BrowserWidth' });
    latestEvent = 'mobile';
  }
  if (window.outerWidth <= 801 && latestEvent !== 'desktop') {
    gtag('event', 'Non-mobile browser width', {
      event_category: 'BrowserWidth',
    });
    latestEvent = 'desktop';
  }
});

Or a boolean for isMobile would also work.或者isMobile的 boolean 也可以。

You need to implement some kind of debounce function你需要实现某种去抖 function

NOTE: this have been updated to include part of @rob bailey's answer.注意:这已更新为包含@rob bailey 的部分答案。 Here is a working jsfiddle.这是一个工作的jsfiddle。

The advantage to this approach is you really only want to fire the function once....not multiple times.这种方法的优点是您真的只想触发 function 一次......而不是多次。 That's a waste of cycles and can lead to poor performance.这是对周期的浪费,并可能导致性能不佳。

function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

var isMobileEvent = false;

var myEfficientFn = debounce(function() {
    if (window.outerWidth > 800 && !isMobileEvent) {
    gtag('event', 'Mobile browser width', { event_category: 'BrowserWidth' });
    isMobileEvent = true;
  }
  if (window.outerWidth < 801 && isMobileEvent) {
    gtag('event', 'Non-mobile browser width', {
      event_category: 'BrowserWidth',
    });
    isMobileEvent = false;
  }
}, 250);

window.addEventListener('resize', myEfficientFn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM