在不使用外部库的情况下向JavaScript代码添加简单延迟

在不使用外部库的情况下向JavaScript代码添加简单延迟,javascript,delay,Javascript,Delay,一个快速的问题,请告诉我如何增加延迟这个代码 我想这非常简单,但我对JavaScript还是新手。 我认为答案就在持续时间变量的开始部分 下面是JavaScript代码: var modern = requestAnimationFrame, duration = 400, initial, aim; window.smoothScroll = function(target) { var header = document.querySelectorAll('.aconmineli');

一个快速的问题,请告诉我如何增加延迟这个代码

我想这非常简单,但我对JavaScript还是新手。 我认为答案就在持续时间变量的开始部分

下面是JavaScript代码:

var modern = requestAnimationFrame, duration = 400, initial, aim;

window.smoothScroll = function(target) {
var header = document.querySelectorAll('.aconmineli');
    aim = -header[0].clientHeight;
    initial = Date.now();

  var scrollContainer = document.getElementById(target);
  target = document.getElementById(target);

    do {
    scrollContainer = scrollContainer.parentNode;
    if (!scrollContainer) return;
    scrollContainer.scrollTop += 1;
    }
    while (scrollContainer.scrollTop == 0);

    do {
    if (target == scrollContainer) break;
    aim += target.offsetTop;
    }
    while (target = target.offsetParent);

    scroll = function(c, a, b, i) {

        if (modern) {
        var present = Date.now(),
        elapsed = present-initial,
        progress = Math.min(elapsed/duration, 1);
        c.scrollTop = a + (b - a) * progress;
        if (progress < 1) requestAnimationFrame(function() {
        scroll(c, a, b, i);
        });
        }
        else {
        i++; if (i > 30) return;
        c.scrollTop = a + (b - a) / 30 * i;
        setTimeout(function() {scroll(c, a, b, i)}, 20);
        }
    }

    scroll(scrollContainer, scrollContainer.scrollTop, aim, 0);
}
var modern=requestAnimationFrame,持续时间=400,初始,目标;
window.smoothScroll=函数(目标){
var header=document.queryselectoral('.aconmineli');
aim=-header[0].clientHeight;
初始=日期。现在();
var scrollContainer=document.getElementById(目标);
target=document.getElementById(目标);
做{
scrollContainer=scrollContainer.parentNode;
如果(!scrollContainer)返回;
scrollContainer.scrollTop+=1;
}
while(scrollContainer.scrollTop==0);
做{
如果(目标==scrollContainer)中断;
aim+=target.offsetTop;
}
while(target=target.offsetParent);
滚动=功能(c、a、b、i){
国际单项体育联合会(现代){
var present=Date.now(),
已用=当前首字母,
进度=数学分钟(已用/持续时间,1);
c、 scrollTop=a+(b-a)*进度;
if(进度<1)requestAnimationFrame(函数(){
卷轴(c、a、b、i);
});
}
否则{
i++;如果(i>30)返回;
c、 scrollTop=a+(b-a)/30*i;
setTimeout(函数(){scroll(c,a,b,i)},20);
}
}
滚动(scrollContainer,scrollContainer.scrollTop,aim,0);
}
顺便说一句,这是一个很好的纯JavaScript代码,可以在点击时滚动。

var modern=requestAnimationFrame,
var modern = requestAnimationFrame,
    duration = 400,
    initial,
    aim,
    delay = 1000;

window.smoothScroll = function(target) {
    setTimeout(function() {
        window.doSmoothScroll(target);
    }, delay);
};

window.doSmoothScroll = function(target) {
    var header = document.querySelectorAll('.navbar');
    aim = -header[0].clientHeight;
    initial = Date.now();

    var scrollContainer = document.getElementById(target);
    target = document.getElementById(target);

    do {
        scrollContainer = scrollContainer.parentNode;
        if (!scrollContainer) return;
        scrollContainer.scrollTop += 1;
    }
    while (scrollContainer.scrollTop === 0);

    do {
        if (target == scrollContainer) break;
        aim += target.offsetTop;
    }
    while (target == target.offsetParent);

    scroll = function(c, a, b, i) {

        if (modern) {
            var present = Date.now(),
                elapsed = present - initial,
                progress = Math.min(elapsed / duration, 1);
            c.scrollTop = a + (b - a) * progress;
            if (progress < 1) requestAnimationFrame(function() {
                scroll(c, a, b, i);
            });
        } else {
            i++;
            if (i > 30) return;
            c.scrollTop = a + (b - a) / 30 * i;
            setTimeout(function() {
                scroll(c, a, b, i);
            }, 20);
        }
    };

    scroll(scrollContainer, scrollContainer.scrollTop, aim, 0);
};
持续时间=400, 首字母, 目标 延迟=1000; window.smoothScroll=函数(目标){ setTimeout(函数(){ window.doSmoothScroll(目标); },延误); }; window.doSmoothScroll=函数(目标){ var header=document.querySelectorAll('.navbar'); aim=-header[0].clientHeight; 初始=日期。现在(); var scrollContainer=document.getElementById(目标); target=document.getElementById(目标); 做{ scrollContainer=scrollContainer.parentNode; 如果(!scrollContainer)返回; scrollContainer.scrollTop+=1; } 而(scrollContainer.scrollTop==0); 做{ 如果(目标==scrollContainer)中断; aim+=target.offsetTop; } while(target==target.offsetParent); 滚动=功能(c、a、b、i){ 国际单项体育联合会(现代){ var present=Date.now(), 已用=当前-初始, 进度=数学分钟(已用/持续时间,1); c、 scrollTop=a+(b-a)*进度; if(进度<1)requestAnimationFrame(函数(){ 卷轴(c、a、b、i); }); }否则{ i++; 如果(i>30)返回; c、 scrollTop=a+(b-a)/30*i; setTimeout(函数(){ 卷轴(c、a、b、i); }, 20); } }; 滚动(scrollContainer,scrollContainer.scrollTop,aim,0); };
更改第
setTimeout(function(){scroll(c,a,b,i)},20)行的
20
谢谢你的建议,但它不起作用,已经试过一次了,现在我试了两次。。。仍然没有改变…我同意改变setTimeout最后一个参数就可以了。您必须考虑的是,该参数是毫秒。因此,如果你把它从20改为21,你不会注意到什么。把它改为2000年,让我们看看发生了什么我不知道你想要实现什么样的延迟。将整个函数体包装成
setTimeout(…,1000)谢谢你,但是我已经告诉你我试过两次了,我知道这是毫秒。Drinovc请您更具体地说明我需要如何包装以及包装什么?我已经尝试了几种使用setTimeout(function(){………},3000)的方法;可能是这样的,我想你应该看看JSFIDLE,当我更新你的代码时,它不起作用。我忘了传递论点。更新。