Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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 setInterval/clearInterval的问题_Javascript - Fatal编程技术网

Javascript setInterval/clearInterval的问题

Javascript setInterval/clearInterval的问题,javascript,Javascript,我有一些代码,我正试图为画廊使用,它从数组中获取URL,然后更改页面上的图像。开始画廊很好 var move = window.setInterval(function(){gallery_switch_script()},2000); 但是当我试图在一个函数中清除它时,它不会清除间隔,但是函数的其余部分会工作,但它只工作一次 function bwd(){ clearInterval(move); var move = setInterval(function(){gallery_switc

我有一些代码,我正试图为画廊使用,它从数组中获取URL,然后更改页面上的图像。开始画廊很好

var move = window.setInterval(function(){gallery_switch_script()},2000);
但是当我试图在一个函数中清除它时,它不会清除间隔,但是函数的其余部分会工作,但它只工作一次

function bwd(){
clearInterval(move);
var move = setInterval(function(){gallery_switch_script()},2000);
var b = i-1;
var valueBwd = array[b];
$(".galleryImg").fadeOut(1500,function(){$(".galleryImg").attr("src",valueBwd);});
$(".galleryImg").fadeIn(1500);
}

这完全没有错误。除了包含clearInterval()的快进按钮之外,这似乎是代码中唯一不想工作的部分


这是我所有的代码。

因为
move
是一个局部变量,而不是全局变量

window.onload=function(){var move = window.setInterval(function()     {gallery_switch_script()},2000);};
它是在window.onload“scope”中定义的,在其外部无法访问

因此,在函数外定义move,它就会起作用

var move;
window.onload=function(){move = window.setInterval(function()     {gallery_switch_script()},2000);};


你有一个
你应该有一个
{
我意识到,正如我添加的一样,它没有改变clearInterval。不幸的是,这对我来说也不起作用,clearInterval(move)仍然不起作用,也没有吐出任何错误。因为你在第二个函数中再次声明了作为本地函数的move。
var move;
window.onload=function(){move = window.setInterval(function()     {gallery_switch_script()},2000);};
function bwd();
    clearInterval(move);
    move = setInterval(function(){gallery_switch_script()},2000);
    var b = i-1;
    var valueBwd = array[b];
    $(".galleryImg").fadeOut(1500,function(){$(".galleryImg").attr("src",valueBwd);});
    $(".galleryImg").fadeIn(1500);
}