2010-09-28 49 views
0

嗨,我有一個方法,每隔幾秒就改變一個div的背景,就像一個旋轉木馬,當用戶滾過一定的文本位。在#h2Create mouseleave事件中,我想取消更改的Carosel方法(停止播放傳送帶)。我怎樣才能做到這一點??取消jquery方法在mouseleave

$(document).ready(function() { 


    $("#h2Create").mouseenter(function() { 
     changingCarosel('create'); 
     $("#h2Create").css("color", "#99eee5"); 
     $("#ServiceDesc").text('WEB/MOBILE/APPS/BESPOKE DESIGN AND BUILD'); 

    }); 
    $("#h2Create").mouseleave(function() { 

     $("#h2Create").css("color", "#fff"); 
    }); 
}); 

changingCaroselvar bgChangeTimer = {}; 
var bgSecondRotation = {}; 
var bgThirdRotation = {}; 
var bgFourthRotation = {}; 
function changingCarosel(param) { 


    switch (param.toString()) 
    { 
    case 'create': 
    $("#ServicesBackgroundImage").css("background-image", "url(/images/spreadVenture.jpg)"); 
     bgChangeTimer = $.timer(2700, function() { 

     $("#ServicesBackgroundImage").css("background-image", "url(/images/Spreadthorntons.jpg)"); 

     bgSecondRotation = $.timer(2700, function() { 

      $("#ServicesBackgroundImage").css("background-image", "url(/images/spreadC2K.jpg)"); 
      bgSecondRotation = $.timer(2700, function() { 
       changingCarosel("create"); 
      }); 
     }); 

    }); 
    break; 
case 'buzz': 
    $("#ServicesBackgroundImage").css("background-image", "url(/images/jr.jpg)"); 
    bgChangeTimer = $.timer(2700, function() { 

     $("#ServicesBackgroundImage").css("background-image", "url(/images/vufold.jpg)"); 

     bgSecondRotation = $.timer(2700, function() { 

      $("#ServicesBackgroundImage").css("background-image", "url(/images/prmothean.jpg)"); 
      bgThirdRotation = $.timer(2700, function() { 
       $("#ServicesBackgroundImage").css("background-image", "url(/images/dayParade.jpg)"); 
       bgFourthRotation = $.timer(2700, function() { 
        changingCarosel("buzz"); 
       }); 
      }); 
     }); 

    }); 
    break; 
default: 
    $("#ServicesBackgroundImage").css("background-image", "url(/images/ifIruledTheWorld.jpg)"); 
    bgChangeTimer = $.timer(2700, function() { 

     $("#ServicesBackgroundImage").css("background-image", "url(/images/Spreadthorntons.jpg)"); 

     bgSecondRotation = $.timer(2700, function() { 

      $("#ServicesBackgroundImage").css("background-image", "url(/images/spreadC2K.jpg)"); 
      bgSecondRotation = $.timer(2700, function() { 
       changingCarosel("spread"); 
      }); 
     }); 

    }); 
    break; 
    } 
} 
+0

我的問題已被打破在這裏由我有開關案件。它們正被用於確定用戶在哪些文本上徘徊。 – 2010-09-28 11:39:57

回答

0

我不承認$ .timer()函數。它是一些插件嗎?我找不到任何文檔。

如果它返回一個普通的javascript定時器,使用setInterval或setTimeout創建,您可以使用clearInterval(bgChangeTimer)來取消它;或clearTimeout(bgChangeTimer);分別。

檢查您正在使用的插件的文檔。

否則,您可以試用Cycle插件。它很容易和靈活地做到這一點: http://jquery.malsup.com/cycle/

+0

是的,它是一個jQuery的定時器插件:http://www.evanbyrne.com/article/jquery-timer-plugin忘了這一點。我只是嘗試設置時間間隔而不是使用這個。 – 2010-09-28 11:45:21

+0

我正在使用的定時器插件是使用setTimeout創建的。所以我嘗試了我的mouseleave方法: clearTimeout(bgChangeTimer); clearTimeout(bgSecondRotation); clearTimeout(bgThirdRotation); clearTimeout(bgFourthRotation); 但是沒有運氣:-(對於這種情況,循環會更好嗎? – 2010-09-28 11:55:29

+0

「會在這種情況下循環更好嗎?」好吧,它工作起來很容易設置,試試吧! – geon 2010-09-28 16:06:48