0

我会尽力清楚地解释我的问题。所以我制作了一堆显示值并保持这些值最新的javascript函数,我需要一个间隔来刷新函数。

这些是我区间内的功能

var interval = null; //make global variable for interval

function refresh(){      // create a function that contains all the functions that need to be refreshed so I can pass this function to my setInterval
        g1.refresh(linearScaling(0, 32761 , 0 , 10, readData('IW0')));
        showAnalogText('textonly', 'IW0');
        g2.refresh(linearScaling(0, 32761 , 0 , 10, readData('IW1')));
        showAnalogText('textonly2', 'IW1');
        showDigInputImg('light1', 'IX2.0', '/imgs/lightON.png', '/imgs/lightOFF.png');
        showDigInputImg('light2', 'IX2.1', '/imgs/lightON.png', '/imgs/lightOFF.png');
        showDigInputBool('led1', 'IX2.0', 'radio');
        showDigInputBool('led2', 'IX2.1', 'radio');
        showDigInputBool('boolean1', 'IX2.0', 'bool');
        showDigInputBool('boolean2', 'IX2.1', 'bool');
        showDigInputBool('text1', 'IX2.0', 'text');
        showDigInputBool('text2', 'IX2.1', 'text');
        showDigInputImg('output1', 'QX2.0', '/imgs/lightON.png', '/imgs/lightOFF.png');
        showDigInputImg('output2', 'QX2.1', '/imgs/lightON.png', '/imgs/lightOFF.png');
        }, 1000)

所以这些函数需要不断更新,它们使用ajax执行读取请求,但我也有写入函数,我想要完成的是在我执行写入函数时停止间隔,所以ajax请求停止并且我可以执行我的写请求,当写完成时,它会重新启动间隔。

这些是我的启动和停止功能:

        function startRefresh(){  //needs to be declared in my library but the function refresh() doesn't exist in library
        if (!interval){
            interval = setInterval(refresh, 1000);
        }
    }

    function stopRefresh(){  

            clearInterval(interval);
            interval = null;
    }

我知道这可行,但问题是我正在构建一个库,以便用户可以重新使用我的函数供自己使用,因此 startrefresh 和 stoprefresh 函数将在库中,但 startrefresh 函数需要 refresh() (包含所有那些其他函数)但刷新只能在 HTML 页面本身的 javascript 中声明(而不是在库中),因为函数 refresh 包含在运行时创建的 g1 和 g2 等对象,所以我不能声明刷新() 在我的库中,因为它包含尚不存在的对象。

谁能帮我找到一个简单的解决方案,你可以改变你想要的任何东西,我只想要最有效的方式,用户可以将他需要刷新的所有功能放在一个间隔中,并在我执行写入时停止和启动间隔,但是 stop 和 startrefresh 函数需要在库中,用户不应该看到 start 和 stop 函数,它们在我的 write 函数中被调用。希望有人能理解并帮助我,我将非常感激,如果您需要更多解释,我很乐意给您

也许将所有函数放在某种变量中并将该变量传递给我的 startrefresh 函数,这行得通吗?

和平,斯蒂恩

4

1 回答 1

1

为什么不使用函数作为参数startRefresh

function startRefresh(refresh) {
    if (!interval){
        interval = setInterval(refresh, 1000);
    }
}

因此您可以使用匿名函数运行该函数

startRefresh(function() {
   //do stuff to be refreshed
});

或者在您的情况下,您也可以在匿名功能中存储刷新功能

startRefresh(function(){
    g1.refresh(linearScaling(0, 32761 , 0 , 10, readData('IW0')));
    showAnalogText('textonly', 'IW0');
    g2.refresh(linearScaling(0, 32761 , 0 , 10, readData('IW1')));
    showAnalogText('textonly2', 'IW1');
    showDigInputImg('light1', 'IX2.0', '/imgs/lightON.png', '/imgs/lightOFF.png');
    showDigInputImg('light2', 'IX2.1', '/imgs/lightON.png', '/imgs/lightOFF.png');
    showDigInputBool('led1', 'IX2.0', 'radio');
    showDigInputBool('led2', 'IX2.1', 'radio');
    showDigInputBool('boolean1', 'IX2.0', 'bool');
    showDigInputBool('boolean2', 'IX2.1', 'bool');
    showDigInputBool('text1', 'IX2.0', 'text');
    showDigInputBool('text2', 'IX2.1', 'text');
    showDigInputImg('output1', 'QX2.0', '/imgs/lightON.png', '/imgs/lightOFF.png');
    showDigInputImg('output2', 'QX2.1', '/imgs/lightON.png', '/imgs/lightOFF.png');
    }, 1000)
});

更新

function makeStartRefresher(refresh) {
    return function() {
        if (!interval){
            interval = setInterval(refresh, 1000);
        }
    }
}

您可以使用此功能:

var startRefresh = makeStartRefresher(function() {
    g1.refresh(linearScaling(0, 32761 , 0 , 10, readData('IW0')));
    showAnalogText('textonly', 'IW0');
    g2.refresh(linearScaling(0, 32761 , 0 , 10, readData('IW1')));
    showAnalogText('textonly2', 'IW1');
    showDigInputImg('light1', 'IX2.0', '/imgs/lightON.png', '/imgs/lightOFF.png');
    showDigInputImg('light2', 'IX2.1', '/imgs/lightON.png', '/imgs/lightOFF.png');
    showDigInputBool('led1', 'IX2.0', 'radio');
    showDigInputBool('led2', 'IX2.1', 'radio');
    showDigInputBool('boolean1', 'IX2.0', 'bool');
    showDigInputBool('boolean2', 'IX2.1', 'bool');
    showDigInputBool('text1', 'IX2.0', 'text');
    showDigInputBool('text2', 'IX2.1', 'text');
    showDigInputImg('output1', 'QX2.0', '/imgs/lightON.png', '/imgs/lightOFF.png');
    showDigInputImg('output2', 'QX2.1', '/imgs/lightON.png', '/imgs/lightOFF.png');
    }, 1000)
});
于 2013-03-12T19:33:41.563 回答