utopia
utopia

Reputation: 177

setInterval and function

I have written this simple 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>

Function:

$.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');

I want to add setInterval to this script:

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

But the headline items doesnt change. How can I make change in inters function every 3000 ms?

Thanks in advance

Upvotes: -1

Views: 538

Answers (2)

stecb
stecb

Reputation: 14746

you need to do something in this way:

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

setInterval(foo,3000);

Upvotes: 0

Kevin
Kevin

Reputation: 5694

Your setInterval will attempt to call the function inters() every 3 seconds, which is probably not what you want.

What I think you want to execute is the following line:

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

If you want to do so you should create a function that will execute that line, like so:

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

The above will call $('#topt').inters('.headline'); every 3 seconds.

Upvotes: 2

Related Questions