SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery Problem with serInterval

    I have written this script:
    Code:
    $.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();
    
    
    function headline(){
      $("#topt").inters('.headline');
    }
    
    setInterval(headline,3000);
    And HTML:

    Code:
    <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>
    the hover effect doesnt repeat with sertInterval. Why sertInterval doesnt work here?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    It's most likely due to a syntax error within your code.

    Find the end of the click function, and then try to find the end of the inters function.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you tried ?

    Code JavaScript:
    function headline(){
      $("#topt").inters('.headline');
     $("#topt").hover();
    }

    But first you need to make sure the inters function run without the setInterVal(). Then try my suggestion.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runrunforest View Post
    have you tried ?
    It seems to be a simpler problem than that.
    There are opening braces for two of the functions, but only one closing brace.

    Proper indentation will clearly show where the closing brace needs to be placed.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks paul;

    with :
    Code:
    $.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();
    
    }
    function headline(){
      $("#topt").inters('.headline');
    }
    
    setInterval(headline,3000);
    or
    Code:
    function headline(){
      $("#topt").inters('.headline');
     $("#topt").hover();
    }
    the hover effect doesnt repeat. Is there any logic fault in my function?

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on line 2, "this" should be "$(this)". Change that, and let us know if there is error. Post the error message.

    It's really to debug JavaScript with firebug, if you haven't got it, you can download from here Firebug . You need to have firefox in order to use firebug.

    Once you've installed firebug, open it and refresh the page, if there is error, you can see a red message in firebug pane.

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And should tell us what you are trying archive, maybe someone can write up something that can make sense for you.

  8. #8
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    In above image, 1 2 3 navigation is a list, top section's data is data from li's rel and a' title. With hover, content changes with hover on navigation. The html is:

    Code:
    <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="lipsum1" href="">1</a> </li>
      <li title="http://www.site.com/761028497.jpg"> <a title="lipsum2" href="">2</a> </li>
     </ul>
    </div>
    I use firebug and firefox. There is not error in javascript (I have tested with chrome developer tool too) I want to change 1 2 3 automatic. But I cant with setInterval

    Thanks

  9. #9
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I see, there a are plenty of premade script to archieve this, just google "image slide with hover".

    I can make simple one for you, but it's time to off to bed, maybe tomorrow. but do let us know if you found the pre-made script.

    And by the way, your html is missing a closing div. 3 divs but there are only 2 closing div

  10. #10
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    And by the way, your html is missing a closing div. 3 divs but there are only 2 closing div
    Yes I have seen this but I cant edit...
    I have found many scripts like this but all of them, load all items in top div and show, hide them, but in my script content loads with hover.
    For example if you have 20 item in navigation, all of 20 content doesnt load at once and only load by hover.
    This script works very good but has not automation.
    BTW, Im newbie in js...
    Many thanks for your help.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •