Javascript: how to stop the click event queuing?

心不动则不痛 提交于 2019-12-20 07:32:51

问题


The following code works fine the only problem is that the click event get queued like for example the setTimeout gets called for each click and the popup appears multiple times. How to make the popup appear only when user clicks and to ensure that gap between each popup is lets say 4 seconds

var showpopup = 1;
var check = true;
var seconds = 4000;             // change the frequency here current frequency = 2 seconds
var url = "https://www.fb.com"; // change the url here
var strWindowFeatures = "toolbar=yes,scrollbars=yes,resizable=yes,top=0,left=0,width=" +screen.width 
                         + ",height=" +screen.height;
function popup (event)
{   
     event.stopPropagation();
     if (showpopup === 1)
     {
          //if 
(navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Windows 
Phone|Opera Mini|IEMobile/i))
          //{
                if (check == true)
                {
                      window.open(url , '_blank' , strWindowFeatures);
                      showpopup++;
                      check = false;
                }
          //}
     }
     else
     {
         setTimeout(checkValue, seconds);   
     }
}

function checkValue ()
{
    showpopup = 1;
    check = true;
}

window.onclick = popup(event);

回答1:


This is called function throttling :

function throttle(func){
  var timeout=null;
  return function(...args){
    if(!timeout) func(...args), timeout=setTimeout(_=>timeout=null,4000);//if were not waiting, execute the function and start waiting
  }
}

So you can do

var alert=throttle(window.alert);

alert("Wohoo!");
alert("Never executed!");

In your case it would be:

window.onclick = throttle(popup);

My Syntax is maybe a bit complicated (ES6), so lets explain it a bit:

return function(...args){

return a new function that stores all arguments in the args Array ( So that we can pass it to our inner function)

    if(!timeout) 

If theres no Timeout

func(...args), 

call the function, passing our args array as parameters ( called spread operator)

timeout=setTimeout(...,4000)

set our timeout to execute after 4000 to execute the following:

_=>timeout=null

When the timeout finishes, reset the timeout and wait for the next function call...




回答2:


Lodash has a throttle function for this.

_.throttle(func, [wait=0], [options={}])



回答3:


The following code solves the problem without using throttle

// JavaScript Document

var showpopup = 1;
var check = true;
var seconds = 4000;             // change the frequency here current frequency = 2 seconds
var url = "https://www.fb.com"; // change the url here
var strWindowFeatures = "toolbar=yes,scrollbars=yes,resizable=yes,top=0,left=0,width=" +screen.width + ",height=" +screen.height;

function popup ()
{   
    if (check === false)
    {   
         return;
    } 
    else if (showpopup === 1)
    {
         if (navigator.userAgent.match(/Android|BlackBerry|iPhone|iPad|iPod|Windows Phone|Opera Mini|IEMobile/i))
         {
              if (check == true)
              {
                    window.open(url , '_blank' , strWindowFeatures);
                    showpopup++;
                    check = false;
                    setTimeout(checkValue , seconds);
              }
         }
    }
}

function checkValue ()
{
     showpopup = 1;
     check = true;
}

window.onclick = popup;


来源:https://stackoverflow.com/questions/44289889/javascript-how-to-stop-the-click-event-queuing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!