简体   繁体   English

如何只在第一次调用函数 - JavaScript

[英]How to call a function only for the first time - JavaScript

Note: I need to achieve this with pure javascript, I know there is a .one() method in jquery to do this, but I need the same output in pure javascript. 注意:我需要使用纯javascript实现这一点,我知道在jquery中有一个.one()方法来执行此操作,但我需要在纯javascript中使用相同的输出。

Scenario: I am trying to call a function when a user scrolls and reaches to the 3/4 part or more of the page, but the problem rises when user reaches that part, We all know they can't be pixel perfect so, after the condition is met, the function gets executed per pixel scroll. 场景:当用户滚动并到达页面的3/4部分或更多时,我试图调用一个函数,但是当用户到达那个部分时问题就出现了, 我们都知道它们不能像素完美所以,之后满足条件,每个像素滚动执行该函数。

I want that to execute only once the condition is met, then add a section at the bottom of the page, and then again user should reach the bottom and the function should get executed only once and so on... 我希望只有在满足条件后才执行,然后在页面底部添加一个部分,然后再次用户应该到达底部,函数应该只执行一次,依此类推......

Snippet: 片段:

 var colors = ['skyblue', 'powderblue', 'lightgreen', 'indigo', 'coral']; var addPage = function() { var page = document.createElement('div'); page.classList.add('page'); page.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)]; document.body.append(page); console.log('added a new page'); } var scrollAndAdd = function() { if (window.scrollY < (window.innerHeight * (3 / 4))) { // execute addPage only once for each attempt; it's creating infinite pages addPage(); } } window.addEventListener('scroll', scrollAndAdd); 
 * { margin: 0; padding: 0; } .page { height: 100vh; } 
 <div class='page' style='background-color: lightgreen'></div> <div class='page' style='background-color: skyblue'></div> 

You don't really need logic to run the function just once; 你真的不需要逻辑来运行一次这个函数; instead, use a different expression to determine whether to add the page. 相反,使用不同的表达式来确定是否添加页面。 Once the page is added that same expression should no longer evaluate to true until more scrolling is done. 添加页面后,相同的表达式不应再评估为true,直到完成更多滚动。

NB: I also changed a bit the random pick logic. 注意:我也改变了随机选择逻辑。

 var colors = ['powderblue', 'lightgreen', 'indigo', 'coral', 'skyblue']; var addPage = function() { var page = document.createElement('div'); page.classList.add('page'); // Make sure the same color is not selected twice in sequence: var colorIndex = Math.floor(Math.random() * (colors.length-1)); var color = colors.splice(colorIndex,1)[0]; colors.push(color); page.style.backgroundColor = color; document.body.append(page); } var scrollAndAdd = function() { if (window.scrollY > document.body.clientHeight - window.innerHeight - 10) { addPage(); } } window.addEventListener('scroll', scrollAndAdd); 
 * { margin: 0; padding: 0; } .page { height: 100vh; } 
 <div class='page' style='background-color: lightgreen'></div> <div class='page' style='background-color: skyblue'></div> 

I hope it will help you: 我希望它会对你有所帮助:

 var colors = ['skyblue', 'powderblue', 'lightgreen', 'indigo', 'coral']; var addPage = function() { var page = document.createElement('div'); page.classList.add('page'); page.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)]; document.body.append(page); console.log('added a new page'); } var scrollAndAdd = function() { var a = document.body.clientHeight - window.innerHeight * (5 / 4) if (window.scrollY > a) { addPage(); } } window.addEventListener('scroll', scrollAndAdd); 
 * { margin: 0; padding: 0; } .page { height: 100vh; } 
 <div class='page' style='background-color: lightgreen'></div> <div class='page' style='background-color: skyblue'></div> 

Well how about curry it up with local flag. 好吧,如何用当地的旗帜咖喱。

var colors = ['skyblue', 'powderblue', 'lightgreen', 'indigo', 'coral'];


const localCurry = function(func, immediateAction) {
  var flag;
  return  function() {
    var context = this, args = arguments;
    var callNow = immediateAction && !flag;
    flag = true;
    if (callNow) func.apply(context, args);
  }
}


var addPage = localCurry(function() {
  var page = document.createElement('div');
  page.classList.add('page');
  page.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
  document.body.append(page);
  console.log('added a new page');
}, true);

var scrollAndAdd = function() {
  var a = document.body.clientHeight - window.innerHeight * (5 / 4)
  if (window.scrollY > a) {
    addPage();
  }
}

window.addEventListener('scroll', scrollAndAdd);

Now you do have option to reset the flag based on timer or custom logic. 现在您可以选择根据计时器或自定义逻辑重置标志。

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

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