是她丶
2017-07-17 01:28
采纳率: 52.6%
浏览 1.4k

关于JS封装调用的问题

 FivePage = {};
FivePage.show = function() {
    this.change11 = null;
    this.length = 0;
}
FivePage.show.prototype.Ypage1 = function(id) {
    change11 = setInterval(function() {
        length++;
        if(length == $("#" + id + " li").length) {
            length = 0;
        }
        console.log("第五页定时器");
        console.log(this.length);
        console.log(this.ll);
        showaa(length);
    }, 2000);
    function showaa(length) {
        $("#" + id + " li").eq(length - 1).fadeOut(1000); //将上一张图片隐藏
        $("#" + id + " li").eq(length).fadeIn(1000); //将这张图片出现
    }
}
var cbb = new FivePage.show();
btn1.onclick = function() {
    test11.style.display = "block";
    test12.style.display = "none";
    clearInterval(change11);
    alert("现在的下标是:"+length+"page1");
    cbb.Ypage1("test11");
}
btn2.onclick = function() {
    test12.style.display = "block";
    test11.style.display = "none";
    clearInterval(change11);
    alert("现在的下标是:"+length+"Page2");
    cbb.Ypage1('test12');
};

各位大哥,我现在的问题是,我点击btn2的时候,他的length不会从0开始,我需求是
点击bun1length从0开始走,这个时候,btn1的length走到了3,我在点击btn2的时候,length从0开始走。(这个时候,length会等于3,而不是0),所以问题是他们公用了一个length,我写两个变量,会报错为 not defined
当我再次点击btn1的时候,length会从之前的3继续开始走

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • 已采纳
    
        FivePage = {};
        FivePage.show = function () {
            this.change11 = null;
            this.length = 0;
        }
        FivePage.show.prototype.Ypage1 = function (id) {
            var me = this;////闭包,要不setInterval执行的函数中this为window,用me来访问length
            this.change11 = setInterval(function () {
                me.length++;
                if (me.length == $("#" + id + " li").length) {
                    me.length = 0;
                }
                console.log("第五页定时器");
                console.log(me.length);
                console.log(me.ll);
                showaa(length);
            }, 2000);
            function showaa(length) {
                $("#" + id + " li").eq(length - 1).fadeOut(1000); //将上一张图片隐藏
                $("#" + id + " li").eq(length).fadeIn(1000); //将这张图片出现
            }
        }
        var cbb = new FivePage.show();
        btn1.onclick = function () {
            test11.style.display = "block";
            test12.style.display = "none";
            clearInterval(cbb.change11);///////////////////////
            alert("现在的下标是:" + length + "page1");
            cbb.Ypage1("test11");
        }
        btn2.onclick = function () {
            test12.style.display = "block";
            test11.style.display = "none";
            clearInterval(cbb.change11);////////////////
            alert("现在的下标是:" + length + "Page2");
            cbb.Ypage1('test12');
        };
    
    点赞 打赏 评论
  • 梦昼初PurpleShell 2017-07-17 01:49

    你自己都知道公用了一个变量引起的,定义两个变量不可能会报错的,not defined是因为你没有定义

    点赞 1 打赏 评论
  • 是她丶 2017-07-17 02:48
     FivePage = {};
    FivePage.show = function() {
        this.change11 = null;
        this.length = 0;
    }
    FivePage.show.prototype.Ypage1 = function(id) {
        console.log(this);
        var me = this;
        change11 = setInterval(function() {
            me.length++;
            if(me.length == $("#" + id + " li").length) {
                me.length = 0;
            }
            console.log("第五页定时器");
            showaa(me.length);
        }, 2000);
        function showaa(length) {
            $("#" + id + " li").eq(length - 1).fadeOut(1000); //将上一张图片隐藏
            $("#" + id + " li").eq(length).fadeIn(1000); //将这张图片出现
        }
    }
    var cbb = new FivePage.show();
    btn1.onclick = function() {
        test11.style.display = "block";
        test12.style.display = "none";
        clearInterval(change11);
        cbb.Ypage1("test11");
    }
    btn2.onclick = function() {
        test12.style.display = "block";
        test11.style.display = "none";
        clearInterval(change11);
        cbb.Ypage1('test12');
    };
    
    点赞 打赏 评论
  • lppzyt 2017-07-17 03:35

    this.length改成引用类型,this.array=[];
    array.length = array.length+1;把length改成array.length它试一下。不好的地方就是浪费空间,我是个新手想不出好的办法

    点赞 打赏 评论

相关推荐 更多相似问题