简体   繁体   中英

merge two functions into one

I have been rewriting my script file several times now just to shorten it, ive come quite long with it except with two funcitons,

I wish to merge these to function into one

first of with the pageload function, this initializes on document ready

function pageload(elements, settings) {
elements = $.extend({}, defaultelements, elements || {});
settings = $.extend({}, defaultsettings, settings || {});

if( settings.url === '' ) {
    console.log('No soup for you!');
    movePage.prototype.pagein();
} else {
    $.ajax({
        url: location,
        data: {
            json: 'get_page',
            slug: settings.url
        },
        success: function(data) {
            var html = '<article id="' + data.page.slug + '" data-info="' + data.page.slug + '" class="subpage"></article>';

            if(settings.motion) return false;
                else settings.motion = true;

            elements.thumbs.before(html);
            var container = $('.subpage');
            elements.body.prepend('<div class="spinner"></div>');
            $('.spinner').hide();
            container.load(data.page.url).hide();

            elements.thumbs.stop().animate({
                'top' : '100%'
            }, settings.speed, settings.easing, function() {
                $('.spinner').fadeIn(259);
                setTimeout(function() {
                    $('.spinner').fadeOut(559);
                    setTimeout(function() {
                        container.fadeIn(659);
                    }, 450)
                    setTimeout(function() {
                        $('.spinner').remove();
                    }, 200)
                }, 1250);
                settings.motion = false;
                movePage.prototype.pagein();
            });
        }
    });
}
}

And the there is the navigation json load script

function navigation(elements, settings) {
elements = $.extend({}, defaultelements, elements || {});
settings = $.extend({}, defaultsettings, settings || {});

elements.navitem.live('click', function(event) {
    event.preventDefault();

    $('.current-menu-item').removeClass('current-menu-item');
    $(this).parent().addClass('current-menu-item');

    settings.url = $(this).html();
    $.ajax({
        url: location,
        data: {
            json: 'get_page',
            slug: settings.url
        },
        success: function(data) {
            var current = $('.subpage');

            if( settings.url == 'work' ) {
                settings.motion = false;
            } else if( current.attr('id') == settings.url ) {
                settings.motion = false;
                return;
            } else {
                var html = '<article id="' + data.page.slug + '" data-info="' + data.page.slug + '" class="subpage"></article>';

                if(settings.motion) return false;
                    else settings.motion = true;

                elements.thumbs.before(html);
                var container = $('.subpage');
                elements.body.prepend('<div class="spinner"></div>');
                $('.spinner').hide();
                container.load(data.page.url).hide();

                elements.thumbs.stop().animate({
                    'top' : '100%'
                }, settings.speed, settings.easing, function() {
                    $('.spinner').fadeIn(259);
                    setTimeout(function() {
                        $('.spinner').fadeOut(559);
                        setTimeout(function() {
                            container.fadeIn(659);
                        }, 450)
                        setTimeout(function() {
                            $('.spinner').remove();
                        }, 200)
                    }, 1250);
                    settings.motion = false;
                    movePage.prototype.pagein();
                    $('article:not(#' + data.page.slug + ')').remove();
                });
            }
        }
    });
});
}

Do anybody here have a solution that makes these to functions into one that i can call both on pageload and on nav click.

If I'm understanding your question correctly... - Define a single named function, containing the function calls you need. - create two additional (very short) functions, one that runs on pageload, and calls the larger function, and one that runs on navclick, and calls the larger function

If that is not what you need, then what is it that makes that not work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM