简体   繁体   English

如何在悬停时停止和启动此功能

[英]How do I stop and start this function on hover

I want to stop and start a function on the hover of a container div, I tried myLoop.stop(); 我想在容器div的悬停上停止并启动一个函数,我尝试了myLoop.stop();。 reset the variable but it doesn't work right... Any Thoughts??? 重置变量,但是它不能正常工作...任何想法???

    //Continuous Scroll
var nex = 1;
var timeInterval = 1000;
$(".logoContainer").hover(function(){
    // Stop Function Here

},function () {
    // Start Function Here
})
function myLoop () { //  create a loop function
setTimeout(function () {    //  call a 3s setTimeout when the loop is called
  if( cache.isAnimating ) return false;
  cache.isAnimating = true;
  aux.navigate( 1, $el, $wrapper, settings, cache );
  nex++; 
  if (nex < 100) {
    myLoop(); 
   }  
  }, timeInterval)
 }
myLoop();

I suggest placing a flag inside your setTimeout function and change this flag according to the event that you want, take a look at this: http://api.jquery.com/hover/ 我建议在您的setTimeout函数中放置一个标志,并根据您想要的事件更改此标志,请看一下: http : //api.jquery.com/hover/

Your code might need some refactoring, how about something like this (pseudo-code): 您的代码可能需要一些重构,类似这样的东西(伪代码):

function callThisFunctionEveryXSeconds(){
    if(continueFlag){
        keepScrolling();
    }
}
$(someDiv).mouseenter() { continueFlag = true; }
$(someDiv).mouseleave() { continueFlag = false; }

Use clearTimeout to stop setTimeout . 使用clearTimeout停止setTimeout

var nex = 1;
var timeInterval = 1000;
$(".logoContainer").hover(function(){
        looper.stop();

    },function () {
        looper.start();
    });

function myLoop () { //  create a loop function
    var timeout;
    this.start = function(){
        timeout = setTimeout(function () { 
            cache.isAnimating = true;
            aux.navigate( 1, $el, $wrapper, settings, cache );
            nex++; 
            if (nex < 100) {
                myLoop(); 
            }  
        }, timeInterval);
    }
    this.stop = function () {
        clearTimeout(timeout);
    }
}
var looper = new myLoop()

OK here I had to change the variable that ended the function then reset and fire again on mouseout(hoverOut): 好的,这里我必须更改结束函数的变量,然后重置并在mouseout(hoverOut)上再次触发:

 //Continuous Scroll
var nex = 1,timeInterval = 1000,loop = 0;
function myLoop () { //  create a loop function
    setTimeout(function () {    //  call a 3s setTimeout when the loop is called
        if( cache.isAnimating ) return false;
            cache.isAnimating = true;
            aux.navigate( 1, $el, $wrapper, settings, cache );
            nex++; 
        if (nex < 100) {
            myLoop(); 
        }  
        }, timeInterval)
    }
myLoop();

$(".logoContainer").hover(function(){
    nex = 100;
},(function(){
    nex = 1;
    myLoop();
        })
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM