2011-04-06 102 views
0

這裏真的很簡單,我敢肯定 - 我是一個初學者,他努力將setInterval集成到我的jQuery中。我目前有這個功能,點擊它們可以旋轉兩個圖像。我希望這會自動發生(每隔幾秒鐘),並且似乎無法找到使用setInterval的正確方法。使用setInvernal每隔幾秒調用一次查詢函數?

任何人都可以指向正確的方向嗎?非常感謝您的幫助。

菲利普

$(document).ready(function(){ 
$("#spinitemholder1 .sponsorFlipphil1 img").click(function() { 
      $(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function() { 
       $(this).parent().hide(); 
       $(this).width(0); 
       $("#spinitemholder1 .sponsorFlipphil2 img").animate({ "width": "271px", "margin-left": "0px" }); 
       $("#spinitemholder1 .sponsorFlipphil2").show(); 
      }); 
     }); 
     $("#spinitemholder2 .sponsorFlipphil2 img").click(function() { 
      $(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function() { 
       $(this).parent().hide(); 
       $(this).width(0); 
       $("#spinitemholder1 .sponsorFlipphil1 img").animate({ "width": "271px", "margin-left": "0px" }); 
       $("#spinitemholder1 .sponsorFlipphil1").show(); 
      }); 
     }); 

}); 
+1

它沒有'setInterval'工作嗎?因爲這裏有一個語法錯誤:''#spinitemholder .sponsorFlipphil1 img「 – 2011-04-06 09:15:33

+0

我想你在代碼中留下了一段XSLT。如果對手頭的問題無關緊要,可否請刪除它? – 2011-04-06 09:16:50

+0

是什麼問題?你爲什麼認爲這是'setInterval'的錯? – 2011-04-06 09:17:03

回答

0

試試這個:

var interval = setInterval(function() { 
    $("#spinitemholder1 .sponsorFlipphil1 img").trigger("click"); 
    $("#spinitemholder2 .sponsorFlipphil2 img").trigger("click"); 
}, 5000); 

您必須使用「觸發器」。當用戶點擊其中一個圖像時,我會建議重置間隔。一個簡單的方法是:

var interval = 0; 
function startNewInterval() { 
    clearInterval(interval); // clear previous one 
    interval = setInterval(function() { // start new interval 
    $("#spinitemholder1 .sponsorFlipphil1 img").trigger("click"); 
    $("#spinitemholder2 .sponsorFlipphil2 img").trigger("click"); 
    }, 5000); 
} 

$("#spinitemholder1 .sponsorFlipphil1 img").click(function() { 
    $(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function() { 
    $(this).parent().hide(); 
    $(this).width(0); 
    $("#spinitemholder1 .sponsorFlipphil2 img").animate({ "width": "271px", "margin-left": "0px" }); 
    $("#spinitemholder1 .sponsorFlipphil2").show(); 
    startNewInterval(); 
    }); 
}); 

$("#spinitemholder2 .sponsorFlipphil2 img").click(function() { 
    $(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function() { 
    $(this).parent().hide(); 
    $(this).width(0); 
    $("#spinitemholder1 .sponsorFlipphil1 img").animate({ "width": "271px", "margin-left": "0px" }); 
    $("#spinitemholder1 .sponsorFlipphil1").show(); 
    startNewInterval(); 
    }); 
}); 

startNewInterval(); 
+0

非常感謝,這真的很有幫助。完美的作品。 – Philip 2011-04-06 10:07:50

0

嘗試運行

setInterval(function(){ 
    $('#spinitemholder1 .sponsorFlipphil1 img, #spinitemholder2 .sponsorFlipphil2 img').click(); 
}, 2000); 

,看看它是否工作。 (只是爲了測試代碼,這不是做的建議的方式)

+0

嗨,這似乎並不適用於我測試 – Philip 2011-04-06 09:31:58

+0

@user後,請在您的問題中執行代碼後執行此操作。如果它不起作用,那麼你的原代碼也不應該工作.. – 2011-04-06 09:34:58

+0

道歉,它確實工作! – Philip 2011-04-06 09:39:02

相關問題