Javascript 有没有一种方法可以让你;“冷却时间”;对你的功能有什么影响?

Javascript 有没有一种方法可以让你;“冷却时间”;对你的功能有什么影响?,javascript,html,css,Javascript,Html,Css,我现在正在为一个网站制作一个演示或幻灯片。除了一件事,一切都安排好了。用户可以垃圾邮件幻灯片,从而导致跳过动画。我想为手动跳过幻灯片添加一个冷却时间。但我想不出任何解决办法。谢谢你的帮助 这里有一把小提琴: var图像=[ “网址(https://cdn.discordapp.com/attachments/461567193927385091/534789187560407045/picture1.png)", “网址(https://cdn.discordapp.com/attachmen

我现在正在为一个网站制作一个演示或幻灯片。除了一件事,一切都安排好了。用户可以垃圾邮件幻灯片,从而导致跳过动画。我想为手动跳过幻灯片添加一个冷却时间。但我想不出任何解决办法。谢谢你的帮助

这里有一把小提琴:

var图像=[
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789187560407045/picture1.png)",
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789189162762240/picture2.png)",
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789190500614147/picture3.png)",
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789199837265929/picture4.png)"
];
var num=0;
var间隔=设置间隔(下一步,5000);
函数next(){
var diashow=document.getElementById(“diashow”);
num++;
如果(num>=images.length){
num=0;
}
diashow.style.backgroundImage=图像[num];
}
函数prev(){
var diashow=document.getElementById(“diashow”);
num--;
if(num<0){
num=images.length-1;
}
diashow.style.backgroundImage=图像[num];
}
函数停止(){
间隔时间;
}
函数集(){
间隔=设置间隔(下一步,5000);
}
#diashow{
用户选择:无;
过渡时间:1s;
宽度:600px;
高度:224px;
背景尺寸:600px 224px;
背景位置:中心;
背景图片:url(https://cdn.discordapp.com/attachments/461567193927385091/534789187560407045/picture1.png);
}
#迪亚肖分区{
宽度:300px;
高度:224px;
背景色:透明;
溢出:隐藏;
光标:指针;
显示:内联块;
过渡时间:1s;
不透明度:0.4;
}
#左:悬停{
盒影:嵌入50px 0px 0px 0px白色;
}
#右:悬停{
盒影:嵌入-50px 0px 0px白色;
}

您想到的是“节流”

这个问题有一个解决方案:

无耻地复制粘贴上述内容:

// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
function throttle(func, wait, options) {
  var context, args, result;
  var timeout = null;
  var previous = 0;
  if (!options) options = {};
  var later = function() {
    previous = options.leading === false ? 0 : Date.now();
    timeout = null;
    result = func.apply(context, args);
    if (!timeout) context = args = null;
  };
  return function() {
    var now = Date.now();
    if (!previous && options.leading === false) previous = now;
    var remaining = wait - (now - previous);
    context = this;
    args = arguments;
    if (remaining <= 0 || remaining > wait) {
      if (timeout) {
        clearTimeout(timeout);
        timeout = null;
      }
      previous = now;
      result = func.apply(context, args);
      if (!timeout) context = args = null;
    } else if (!timeout && options.trailing !== false) {
      timeout = setTimeout(later, remaining);
    }
    return result;
  };
};
//返回一个函数,该函数在调用时最多只触发一次
//在给定的时间窗口内。正常情况下,节流功能将运行
//尽可能地多,每次“等待”期间不超过一次;
//但是,如果您想禁用前缘上的执行,请通过
//`leading:false}`。要禁用后缘执行,同上。
功能节流阀(功能、等待、选项){
变量上下文、参数、结果;
var超时=null;
var-previous=0;
如果(!options)options={};
var later=function(){
previous=options.leading==false?0:Date.now();
超时=空;
结果=函数应用(上下文,参数);
如果(!timeout)context=args=null;
};
返回函数(){
var now=Date.now();
如果(!previous&&options.leading==false)previous=now;
var剩余=等待-(现在-以前);
上下文=这个;
args=参数;
如果(剩余等待){
如果(超时){
clearTimeout(超时);
超时=空;
}
以前=现在;
结果=函数应用(上下文,参数);
如果(!timeout)context=args=null;
}如果(!timeout&&options.training!==false),则为else{
超时=设置超时(稍后,剩余);
}
返回结果;
};
};

只需在代码中添加一个
延迟
和一个'lastClick'变量即可。我已经测试过了,它正在工作:

var delay = 800;
var lastClick = 0;
var images = [
    "url(https://cdn.discordapp.com/attachments/461567193927385091/534789187560407045/picture1.png)",
    "url(https://cdn.discordapp.com/attachments/461567193927385091/534789189162762240/picture2.png)",
    "url(https://cdn.discordapp.com/attachments/461567193927385091/534789190500614147/picture3.png)",
    "url(https://cdn.discordapp.com/attachments/461567193927385091/534789199837265929/picture4.png)"];
var num = 0;
var interval = setInterval(next, 5000);
function next(){
    console.log(lastClick);
    if (lastClick >= (Date.now() - delay))
        return;
    lastClick = Date.now();
    var diashow = document.getElementById("diashow");
    num++;
    if(num >= images.length){num = 0;}diashow.style.backgroundImage = images[num];}
function prev(){
    if (lastClick >= (Date.now() - delay))
        return;
    lastClick = Date.now();
    var diashow = document.getElementById("diashow");
    num--;
    if(num < 0) {num = images.length-1;}diashow.style.backgroundImage = images[num];}
function stop(){clearInterval(interval);}
function set(){interval = setInterval(next, 5000);}
var延迟=800;
var lastClick=0;
变量图像=[
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789187560407045/picture1.png)",
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789189162762240/picture2.png)",
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789190500614147/picture3.png)",
“网址(https://cdn.discordapp.com/attachments/461567193927385091/534789199837265929/picture4.png)"];
var num=0;
var间隔=设置间隔(下一步,5000);
函数next(){
console.log(最后单击);
如果(lastClick>=(Date.now()-delay))
回来
lastClick=Date.now();
var diashow=document.getElementById(“diashow”);
num++;
如果(num>=images.length){num=0;}diashow.style.backgroundImage=images[num];}
函数prev(){
如果(lastClick>=(Date.now()-delay))
回来
lastClick=Date.now();
var diashow=document.getElementById(“diashow”);
num--;
如果(num<0){num=images.length-1;}diashow.style.backgroundImage=images[num];}
函数stop(){clearInterval(interval);}
函数集(){interval=setInterval(next,5000);}
请随意编辑
delay
变量


注:
var
关键字已过时,请查看
let
const

我不确定这是否有效,我也是javascript的学习者。 假设有一个名为
click1()
的按钮单击函数,还有一个名为
loadclick1()
的函数,简而言之

function loadclick1() {
    if (//button-clicked) {
        function click1() {
            //Animation code here;
            setTimeout(loadclick1(), 3000) //This will set a timeout until the function is ready
        }
    }
}

我也有同样的想法,只是为了让按钮触发的函数冷却(以避免以编程方式或非编程方式对服务器进行垃圾处理)

我的问题是不允许通过javascripting访问冷却变量

我是这样做的:

const buttonFunction=(函数设置(){
//const阻止重新分配
常数冷却时间=5000//5s冷却时间
让lastClick=Date.now()-coolDown//重新开始
函数STARTCOLDOWN(){
lastClick=Date.now()//可能是无用的函数
}
函数检查冷却时间(){
const notOver=Date.now()
if(notOver)警报('停止向服务器发送垃圾邮件!')
//使用警报将阻止javascript循环
回来!不要
}
返回函数(参数){
if(检查冷却(){
StartColdown()
//在这里用论点做你的东西
}
}
})()//都是va