Вопрос

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.

Это было полезно?

Решение

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);

Другие советы

You have to change to this

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

_

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top