$changeColor_SetInterval

by mtambulut

JavaScript

toggle_color("#61beb3", "#90a2c6", 4000, 2000);

function toggle_color(color1, color2, cycle_time, wait_time) {

    setInterval(function first_color() {
        document.body.style.backgroundColor = color1;
        setTimeout(change_color, wait_time);
    }, cycle_time);

    function change_color() {
        document.body.style.backgroundColor = color2;
    }
}
/*

window.onload = function() {
    var currentColor = 'red';
    setInterval(function() {
        document.body.style.backgroundColor = currentColor;
        currentColor = currentColor === 'red' ? 'green' : 'red';
    }, 1000);
};

*/