1

By restarting i mean reset it so that it will restart the time, so that it takes 3 seconds from the restart.

My code is:

/* ********** Forside Slider ********** */

var slider_current_id = 1;

$(".slidermenu").live("click", function() {
    var id = $(this).attr('data-id');
    clearInterval(window.slider_timer);
    window.slider_timer = setInterval(start_slider_timer(), 3000);
    window.slider_current_id = parseInt(id, 10);
    $('.slidermenu.currentone').animate({backgroundColor: '#FCFBFB'}, 'slow');
    $('.slidermenu.currentone').removeClass("currentone");
    $(this).animate({backgroundColor: '#f3f3f3'}, 'slow');
    $(this).addClass("currentone");
    $('#activeimage img.currentone').fadeOut('slow');
    $('#activeimage img.currentone').removeClass("currentone");
    $('#activeimage img[data-id='+id+']').fadeIn('slow');
    $('#activeimage img[data-id='+id+']').addClass("currentone");
});

function start_slider_timer() {
    if (window.slider_current_id == 5) {
        window.slider_current_id = 1;
    }else{
        window.slider_current_id = window.slider_current_id + 1;
    }
    $('.slidermenu.currentone').animate({backgroundColor: '#FCFBFB'}, 'slow');
    $('.slidermenu.currentone').removeClass("currentone");
    $('.slidermenu[data-id='+window.slider_current_id+']').animate({backgroundColor: '#f3f3f3'}, 'slow');
    $('.slidermenu[data-id='+window.slider_current_id+']').addClass("currentone");
    $('#activeimage img.currentone').fadeOut('slow');
    $('#activeimage img.currentone').removeClass("currentone");
    $('#activeimage img[data-id='+window.slider_current_id+']').fadeIn('slow');
    $('#activeimage img[data-id='+window.slider_current_id+']').addClass("currentone");
}

var slider_timer = setInterval(start_slider_timer(), 3000);

I dont get any errors, it just doesn't run.. It is my Interval which doesn't run at all.

Hope someone can help me solve this.

6

2 Answers 2

2

Remove the parenthesis to provide the function as parameter instead of calling it. Replace

var slider_timer = setInterval(start_slider_timer(), 3000);

with

var slider_timer = setInterval(start_slider_timer, 3000);

Note that if you want to "restart" the timer, you first have to clear it with

clearInterval(slider_timer);
1
  • Thanks. I replaced var slider_timer = setInterval(start_slider_timer(), 3000); with var slider_timer = setInterval(start_slider_timer, 3000);, and I allready have the clearInterval(slider_timer); on line 8. But without luck. Jan 13, 2013 at 17:29
1

You have to change to this

var slider_timer =  setInterval('start_slider_timer()', 1000);

_

5
  • -1 Huh? Why? This doesn't make sense to me... The only change you made was cutting the time in more then half.
    – user752723
    Jan 13, 2013 at 17:34
  • 2
    @DumbSearch the answer puts the function in quotation marks, which means it will work, though dystroy's solution is probably better.
    – Stuart
    Jan 13, 2013 at 18:00
  • Where should I put this then? Just without the ()? Jan 13, 2013 at 18:05
  • 2
    @ErikHansen The point of this answer is that you put the function inside quotation marks and retain the (). setInterval accepts either a string containing some code, or the name of a function, as its first argument. But if dystroy's answer doesn't work for you then I don't think this one will either.
    – Stuart
    Jan 13, 2013 at 18:08
  • Ok, this seems to make sense. I can only revert my downvote if you edit your answer. Do you mind making a very slight edit to your answer? So I can upvote?
    – user752723
    Jan 13, 2013 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.