簡體   English   中英

setInterval和函數

[英]setInterval and function

我寫了這個簡單的函數:

HTML:

<div class="mhead">
 <div class="mTop"> 
   <a class="headline" title="" href=""> 
     <img alt="" src=""> 
     <span> </span> 
   </a> 
 </div>
<div class="tNav">
 <ul id="topt" class="tUl">
  <li title="http://www.site.com/761028497.jpg"> <a title="" href="">1</a> </li>
  <li title="http://www.site.com/761028497.jpg"> <a title="" href="">2</a> </li>
 </ul>
</div>

功能:

$.fn.inters = function (target) {
    this.find('li').hover(function () {
    var head = $(this).find('a').attr('title');
    var image = $(this).attr('title');
    var href = $(this).find('a').attr('href');
    $(target).find('img').attr('src', image);
    $(target).find('img').attr('alt', head);
    $(target).attr('href', href);
    $(target).attr('title', head);
    $(target).find('span').html(head);
}).eq(0).hover();}

$("#topt").inters('.headline');

我想在此腳本中添加setInterval

$(function() {
setInterval( "inters()", 3000 );
 });

但標題項目並沒有改變。 我怎樣才能使變革inters功能每隔3000毫秒?

提前致謝

你的setInterval將嘗試每隔3秒調用一次函數inters() ,這可能不是你想要的。

認為你想要執行的是以下行:

$('#topt').inters('.headline');

如果你想這樣做,你應該創建一個將執行該行的函數,如下所示:

setInterval(function() {
    $('#topt').inters('.headline');    
}, 3000);

以上將調用$('#topt').inters('.headline'); 每3秒鍾一次。

你需要以這種方式做某事:

function foo(){
    $("#topt").inters('.headline');
}

setInterval(foo,3000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM