防止覆盖运行功能

时间:2013-02-13 00:04:11

标签: javascript

所以我有这个在getDate函数之后运行的Clock,它运行得非常流畅。 我可以开始运行时钟的包装,当点击包装时,它开始用一个事件处理程序移动。 问题是我可以在没有先前停止工作的情况下运行时钟倍增时间。我知道这些函数是相互覆盖的,我一直在尝试移动函数,但是我还没有能够启动乘法时钟。

我有Clock()函数包含其他函数。 我有一个createWrapp(),它动态地创建了函数的类,然后我有一个eventlistner,它在点击包装时启动时钟。

三者是这样的。

function clock(){
    function createWrapp(){
        createElementsWithClass;
    } 
    createWrapp();
    function Wrapp(){
        eventlistner connected to "clock-window-wrapper"
    }
    function myFunction(){ 
        startClock();
    }
}

这是我的jsFiddle。

http://jsfiddle.net/dymond/nufwb/1/

由于

1 个答案:

答案 0 :(得分:2)

问题是<{>> createWrapp中的所有变量都是隐式全局变量。对clock()的连续调用将覆盖它们,并且只有最后创建的时钟才会打勾。使用var statements

将其更改为:

function clock() {

    var outerDiv = createElementWithClass('div', 'clock-window-wrapper'),
        innerDiv = createElementWithClass('div', 'clock-menubar-wrapper');
    outerDiv.appendChild(innerDiv);
    document.getElementById("page-content-wrapper").appendChild(outerDiv);

    var clock_windows_wrapper_close = createElementWithClass('div', 'close');
    innerDiv.appendChild(clock_windows_wrapper_close);

    var clock_toolbar_wrapper_close = createElementWithClass('div', 'clock-content-wrapper');
    outerDiv.appendChild(clock_toolbar_wrapper_close);

    var hours = createElementWithClass('ul', 'clock-digit-wrapper hour');
    clock_toolbar_wrapper_close.appendChild(hours);
    clock_toolbar_wrapper_close.appendChild(document.createTextNode(' ')); // Add this ##WTF

    var minutes = createElementWithClass('ul', 'clock-digit-wrapper minute');
    clock_toolbar_wrapper_close.appendChild(minutes);
    clock_toolbar_wrapper_close.appendChild(document.createTextNode(' ')); // And this ##
    var seconds = createElementWithClass('ul', 'clock-digit-wrapper second');
    clock_toolbar_wrapper_close.appendChild(seconds);

    var hour1 = createElementWithClass('li', "clock-digit-four");
    var hour2 = createElementWithClass('li', "clock-digit-one");
    hours.appendChild(hour1);
    hours.appendChild(hour2);

    var minute1 = createElementWithClass('li', "clock-digit-three");
    var minute2 = createElementWithClass('li', "clock-digit-three");
    minutes.appendChild(minute1);
    minutes.appendChild(minute2);

    var sec1 = createElementWithClass('li', "clock-digit-three");
    var sec2 = createElementWithClass('li', "clock-digit-seven");
    seconds.appendChild(sec1);
    seconds.appendChild(sec2);

createWrapp作为一个单独的函数实际上是不必要的,因为它只被调用一次而且没有变量是局部的。

相关问题