将JQuery函数转换为回调

时间:2015-11-07 05:10:08

标签: jquery

我在jquery中有click方法,但是我需要一个接一个地运行它内部的变量。我之前使用过其他jquery方法的回调函数,但是这里没有使用的方法,它只是调用另一个函数并更改变量值。所以我不知道如何将它们合并以便按顺序运行。是否有任何方法可以使函数在变量值完成更改后才被调用?

   $('#irbis').on('click', function() {
        globalswitchNumber = 0;
        galleryshowUp();
    });

所以我需要globalswitchNumber来完成更改为0,然后galleryshopUp才能执行。因为现在它们是同时执行的。

这是galleryshowUp()函数

 function galleryshowUp() {

            $('.protectiveshield').show(function() {
                $('.mainscreen').hide(0, function() {
                    $('.mainpanel').clearQueue().animate({
                        width: "100px",
                        height: "100px",
                        left: "250px"
                    }, function() {
                        $('.screencontainer').animate({
                            height: "100px",
                            width: "100px"
                        }, 0, function() {
                            $('.mainpanel').animate({
                                width: "805px",
                                height: database[globalswitchNumber].frameheight,
                                paddingTop: "218px"
                            }, function() {
                                $('.topscreen').animate({
                                    top: "-200px"
                                }, 400);
                                $('.screencontainer').delay(0).animate({
                                    height: database[globalswitchNumber].frameheight,
                                    width: "805px"
                                }, 0);


                                $('#galleryone').attr('src', database[globalswitchNumber].galleryone);
                                $('#galleryonelink').attr('href', database[globalswitchNumber].galleryonelink);
                                $('#galleryone').css("display", database[globalswitchNumber].galleryonedisplay);

                                $('#gallerytwo').attr('src', database[globalswitchNumber].gallerytwo);
                                $('#gallerytwolink').attr('href', database[globalswitchNumber].gallerytwolink);
                                $('#gallerytwo').addClass(database[globalswitchNumber].gallerytwodisplay);

                                $('#gallerythree').attr('src', database[globalswitchNumber].gallerythree);
                                $('#gallerythreelink').attr('href', database[globalswitchNumber].gallerythreelink);
                                $('#gallerythree').addClass(database[globalswitchNumber].gallerythreedisplay);

                                $('#galleryfour').attr('src', database[globalswitchNumber].galleryfour);
                                $('#galleryfourlink').attr('href', database[globalswitchNumber].galleryfourlink);
                                $('#galleryfour').addClass(database[globalswitchNumber].galleryfourdisplay);

                                $('#galleryfive').attr('src', database[globalswitchNumber].galleryfive);
                                $('#galleryfivelink').attr('href', database[globalswitchNumber].galleryfivelink);
                                $('#galleryfive').addClass(database[globalswitchNumber].galleryfivedisplay);

                                $('#gallerysix').attr('src', database[globalswitchNumber].gallerysix);
                                $('#gallerysixlink').attr('href', database[globalswitchNumber].gallerysixlink);
                                $('#gallerysix').addClass(database[globalswitchNumber].gallerysixdisplay);

                                $('#galleryseven').attr('src', database[globalswitchNumber].galleryseven);
                                $('#gallerysevenlink').attr('href', database[globalswitchNumber].gallerysevenlink);
                                $('#galleryseven').addClass(database[globalswitchNumber].gallerysevendisplay);

                                $('#galleryeight').attr('src', database[globalswitchNumber].galleryeight);
                                $('#galleryeightlink').attr('href', database[globalswitchNumber].galleryeightlink);
                                $('#galleryeight').addClass(database[globalswitchNumber].galleryeightdisplay);

                                $('#gallerynine').attr('src', database[globalswitchNumber].gallerynine);
                                $('#galleryninelink').attr('href', database[globalswitchNumber].galleryninelink);
                                $('#gallerynine').addClass(database[globalswitchNumber].galleryninedisplay);

                                $('#galleryten').attr('src', database[globalswitchNumber].galleryten);
                                $('#gallerytenlink').attr('href', database[globalswitchNumber].gallerytenlink);
                                $('#galleryten').addClass(database[globalswitchNumber].gallerytendisplay);

                                $('#galleryeleven').attr('src', database[globalswitchNumber].galleryeleven);
                                $('#galleryelevenlink').attr('href', database[globalswitchNumber].galleryelevenlink);
                                $('#galleryeleven').addClass(database[globalswitchNumber].galleryelevendisplay);

                                $('#gallerytwelve').attr('src', database[globalswitchNumber].gallerytwelve);
                                $('#gallerytwelvelink').attr('href', database[globalswitchNumber].gallerytwelvelink);
                                $('#gallerytwelve').addClass(database[globalswitchNumber].gallerytwelvedisplay);

                                $('#gallerythirteen').attr('src', database[globalswitchNumber].gallerythirteen);
                                $('#gallerythirteenlink').attr('href', database[globalswitchNumber].gallerythirteenlink);
                                $('#gallerythirteen').addClass(database[globalswitchNumber].gallerythirteendisplay);

                                $('#galleryfourteen').attr('src', database[globalswitchNumber].galleryfourteen);
                                $('#galleryfourteenlink').attr('href', database[globalswitchNumber].galleryfourteenlink);
                                $('#galleryfourteen').addClass(database[globalswitchNumber].galleryfourteendisplay);

                                $('#galleryfifteen').attr('src', database[globalswitchNumber].galleryfifteen);
                                $('#galleryfifteenlink').attr('href', database[globalswitchNumber].galleryfifteenlink);
                                $('#galleryfifteen').addClass(database[globalswitchNumber].galleryfifteendisplay);

                                $('#gallerysixteen').attr('src', database[globalswitchNumber].gallerysixteen);
                                $('#gallerysixteenlink').attr('href', database[globalswitchNumber].gallerysixteenlink);
                                $('#gallerysixteen').addClass(database[globalswitchNumber].gallerysixteendisplay);

                                $('#galleryseventeen').attr('src', database[globalswitchNumber].galleryseventeen);
                                $('#galleryseventeenlink').attr('href', database[globalswitchNumber].galleryseventeenlink);
                                $('#galleryseventeen').addClass(database[globalswitchNumber].galleryseventeendisplay);

                                $('#galleryeighteen').attr('src', database[globalswitchNumber].galleryeighteen);
                                $('#galleryeighteenlink').attr('href', database[globalswitchNumber].galleryeighteenlink);
                                $('#galleryeighteen').addClass(database[globalswitchNumber].galleryeighteendisplay);

                                $('#gallerynineteen').attr('src', database[globalswitchNumber].gallerynineteen);
                                $('#gallerynineteenlink').attr('href', database[globalswitchNumber].gallerynineteenlink);
                                $('#gallerynineteen').addClass(database[globalswitchNumber].gallerynineteendisplay);

                                $('#gallerytwenty').attr('src', database[globalswitchNumber].gallerytwenty);
                                $('#gallerytwentylink').attr('href', database[globalswitchNumber].gallerytwentylink);
                                $('#gallerytwenty').addClass(database[globalswitchNumber].gallerytwentydisplay);

                                $('#gallerytwentyone').attr('src', database[globalswitchNumber].gallerytwentyone);
                                $('#gallerytwentyonelink').attr('href', database[globalswitchNumber].gallerytwentyonelink);
                                $('#gallerytwentyone').addClass(database[globalswitchNumber].gallerytwentyonedisplay);


                                $('#videoframe').addClass(database[globalswitchNumber].videoframedisplay);
                                $('#videoframe').attr('src', database[globalswitchNumber].videoframe);
                                $('#videoframe').attr('height', database[globalswitchNumber].videoframeheight);

                                $('#captionone').html(database[globalswitchNumber].captionone);
                                $('#captiontwo').html(database[globalswitchNumber].captiontwo);
                                $('#captionthree').html(database[globalswitchNumber].captionthree);


                                setTimeout(function() {
                                    $('#irbiswindow').html($("#tableToClone").clone());
                                }, 0);

                                $('#irbiswindow').show();
                                $('.protectiveshield').hide();



                            });
                        });
                    });
                });
            });

        }

1 个答案:

答案 0 :(得分:0)

创建一个闭包,然后闭包内的变量将受到保护。

$('#irbis').on('click', function() {
    globalswitchNumber = 0;
    function() {
        var localSwitchNumber = globalswitchNumber;
        galleryshowUp(localSwitchNumber);

     }
});

[globalswitchNumber]内将[localSwitchNumber]更改为galleryshowUp(); 如果您在保存全局变量值时遇到问题,可能会在闭包内存储局部变量。除非我能测试完整的代码,否则我无法肯定地说。