Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Clearinterval名称为';t在JS while循环之后定义_Javascript - Fatal编程技术网

Javascript Clearinterval名称为';t在JS while循环之后定义

Javascript Clearinterval名称为';t在JS while循环之后定义,javascript,Javascript,我正在使用以下代码: var s_wrap = document.getElementById('slider'); var s_list = document.getElementById('slider-list'); var li_items = s_list.getElementsByTagName("li"); var next = document.getElementById('next'); var pos, item_w, refreshIntervalId; next.on

我正在使用以下代码:

var s_wrap = document.getElementById('slider');
var s_list = document.getElementById('slider-list');
var li_items = s_list.getElementsByTagName("li");
var next = document.getElementById('next');
var pos, item_w, refreshIntervalId;

next.onclick = function() { 
    item_w = window.getComputedStyle(li_items[0],null).getPropertyValue("width").split('px')[0]; 
    move('left', li_items[0], 10, item_w);
};

var move = function(direction, el, increment, amount) {
    while(pos <= amount){
        keep_moving = setInterval(function(){
            pos = el.style[direction].split('px')[0];
            if(pos == '') pos = 0;
            pos = parseInt(pos) + increment;
            el.style[direction] = pos + 'px';
        }, 100);
    }
    clearInterval(keep_moving); 

};
var move=函数(方向、el、增量、金额){
var保持移动;//
var移动=函数(方向、el、增量、金额){

var keep_moving;//SetInterval重复执行任务。正确的方法是

function move(direction, el, increment, amount) {
    var pos = parseFloat(el.style[direction]) || 0;
    var repeated = function(){
        el.style[direction] = pos + "px";
        pos += increment;
        if(pos < amount) setTimeout(repeated, 100);
    }
    repeated();
}
功能移动(方向、el、增量、金额){
var pos=parseFloat(el.style[方向])| | 0;
var repeated=函数(){
el.样式[方向]=pos+“px”;
pos+=增量;
如果(位置<数量)设置超时(重复,100);
}
重复();
}

顺便说一句,
left
style属性将其向右移动(元素应该离左侧多远)

SetInterval重复执行任务。正确的方法是

function move(direction, el, increment, amount) {
    var pos = parseFloat(el.style[direction]) || 0;
    var repeated = function(){
        el.style[direction] = pos + "px";
        pos += increment;
        if(pos < amount) setTimeout(repeated, 100);
    }
    repeated();
}
功能移动(方向、el、增量、金额){
var pos=parseFloat(el.style[方向])| | 0;
var repeated=函数(){
el.样式[方向]=pos+“px”;
pos+=增量;
如果(位置<数量)设置超时(重复,100);
}
重复();
}

顺便说一句,
left
style属性将其向右移动(元素应该离左侧多远)

您的回答方式不对

您正在生成如此多的间隔,将很快杀死您的网页。您应该尝试这样的逻辑:

var move = function(direction, el, increment, amount) {
    pos = el.style[direction].split('px')[0];
    if(pos == '') pos = 0;
    pos = parseInt(pos) + increment;
    if(pos > amount) { return; }
    el.style[direction] = pos + 'px';
    window.setTimeout(function() { move(direction, el, increment, amount); },100);
};

你回答你的方式不对

您正在生成如此多的间隔,将很快杀死您的网页。您应该尝试这样的逻辑:

var move = function(direction, el, increment, amount) {
    pos = el.style[direction].split('px')[0];
    if(pos == '') pos = 0;
    pos = parseInt(pos) + increment;
    if(pos > amount) { return; }
    el.style[direction] = pos + 'px';
    window.setTimeout(function() { move(direction, el, increment, amount); },100);
};


您的代码是否在严格模式下运行?如果不是,您将创建一个全局变量,该变量应该是可访问的。您不需要在while循环上方声明
var keep\u moving
吗?另外,您确定要在一个循环中创建多个间隔吗?您在每次迭代中都会覆盖
keep\u moving
。@cliffsofinance哦,好的,w你有什么建议?我建议创建一个间隔,并在样式到达其目标时清除它。你的代码是否在严格模式下运行?如果没有,你将创建一个全局变量,该变量应该是可访问的。你不需要在while循环的正上方声明
var keep\u moving
吗?另外,你确定要创建m吗循环中有多个间隔?你在每次迭代中都覆盖
保持移动
。@cliffsof疯狂哦,好吧,你的建议是什么?我建议创建一个间隔,并在样式到达其目标时将其清除。你会建议更好的方法吗?只要继续移动div 10px,直到它向左移动600px…如果我们讨论问题与clearInterval从问题来看,这个答案看起来good@benhowdle89,循环中的间隔似乎是错误的。但这是解决问题的方法…看起来堆栈交换被黑客攻击了。有人用他19.6k的分数为@gdoron写了一个难以置信的愚蠢答案,这对我来说似乎有点可疑。你能提些更好的建议吗继续移动一个div 10px,直到它向左移动600px…如果我们从问题开始用clearInterval讨论这个问题,这个答案看起来good@benhowdle89,循环中的间隔似乎是错误的。但这是解决问题的方法…看起来堆栈交换被黑客攻击了。有人用他的19.6为@gdoron写了一个难以置信的愚蠢答案k点对我来说很可疑。我刚注意到我打错了,你可能想再看一遍。我刚注意到我打错了,你可能想再看一遍