Javascript clearInterval();不起作用
我正在制作一个图像滚动条,它每隔几秒钟(为了调试,10秒钟)或当用户单击图像时自动推进图像。我的代码(如下所示)工作正常,但如果手动(单击)推进图像,我想“重置”10秒计数器。我该怎么做?我似乎在clearInterval上运气不好Javascript clearInterval();不起作用,javascript,jquery,Javascript,Jquery,我正在制作一个图像滚动条,它每隔几秒钟(为了调试,10秒钟)或当用户单击图像时自动推进图像。我的代码(如下所示)工作正常,但如果手动(单击)推进图像,我想“重置”10秒计数器。我该怎么做?我似乎在clearInterval上运气不好 ... setInterval('gallery()',10000); $("#gallery").click(function() { clearInverval();// <--- not working gallery(); }); … 。。。 se
...
setInterval('gallery()',10000);
$("#gallery").click(function() {
clearInverval();// <--- not working
gallery();
});
…
。。。
setInterval('gallery()',10000);
$(“#图库”)。单击(函数(){
clearInverval();//方法返回间隔的句柄,您可以使用该句柄停止间隔:
var handle = window.setInterval(gallery,10000);
$("#gallery").click(function() {
window.clearInterval(handle);
gallery();
});
setInterval
方法返回间隔句柄,您可以使用该句柄停止间隔:
var handle = window.setInterval(gallery,10000);
$("#gallery").click(function() {
window.clearInterval(handle);
gallery();
});
您需要将setInterval设置为一个变量,以使其工作
var interval = setInterval(gallery, 10000);
$('#gallery').click(function() {
clearInterval(interval);
});
您需要将setInterval设置为一个变量,以使其工作
var interval = setInterval(gallery, 10000);
$('#gallery').click(function() {
clearInterval(interval);
});
也许你可以这样做:
var handle = setInterval(gallery,10000);
$("#gallery").click(function() {
clearInterval(handle);
/*
* your #gallery.click event-handler code here
*/
//finally, set automatic scrolling again
handle = setInterval(gallery,10000);
});
也许你可以这样做:
var handle = setInterval(gallery,10000);
$("#gallery").click(function() {
clearInterval(handle);
/*
* your #gallery.click event-handler code here
*/
//finally, set automatic scrolling again
handle = setInterval(gallery,10000);
});
在进行基于原型的编码时,我很难创建一个简单的暂停/继续处理程序,因此不想使用任何外部插件
下面的代码非常简单,希望能节省其他程序员的时间
非功能性示例:
/** THIS DID NOT WORK
function Agent( name )
{
this.name = name;
this.intervalID = undefined; // THIS DID NOT WORK!!!
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
this.intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( this.intervalID );
console.log( "pause" );
}; // pause
**/
var intervalID = undefined;
function Agent( name )
{
this.name = name;
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( intervalID );
console.log( "pause" );
}; // pause
相反,您必须这样做:
/** THIS DID NOT WORK
function Agent( name )
{
this.name = name;
this.intervalID = undefined; // THIS DID NOT WORK!!!
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
this.intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( this.intervalID );
console.log( "pause" );
}; // pause
**/
var intervalID = undefined;
function Agent( name )
{
this.name = name;
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( intervalID );
console.log( "pause" );
}; // pause
在进行基于原型的编码时,我很难创建一个简单的暂停/继续处理程序,因此不想使用任何外部插件
下面的代码非常简单,希望能节省其他程序员的时间
非功能性示例:
/** THIS DID NOT WORK
function Agent( name )
{
this.name = name;
this.intervalID = undefined; // THIS DID NOT WORK!!!
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
this.intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( this.intervalID );
console.log( "pause" );
}; // pause
**/
var intervalID = undefined;
function Agent( name )
{
this.name = name;
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( intervalID );
console.log( "pause" );
}; // pause
相反,您必须这样做:
/** THIS DID NOT WORK
function Agent( name )
{
this.name = name;
this.intervalID = undefined; // THIS DID NOT WORK!!!
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
this.intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( this.intervalID );
console.log( "pause" );
}; // pause
**/
var intervalID = undefined;
function Agent( name )
{
this.name = name;
} // constructor
Agent.prototype.start = function( speed )
{
var self = this;
intervalID = setInterval( function(){ self.act(); }, speed );
}; // start
Agent.prototype.pause = function()
{
clearInterval( intervalID );
console.log( "pause" );
}; // pause
这样做完全破坏了我的点击处理程序=/这样做完全破坏了我的点击处理程序=/