Search code examples
javascripteventsobserver-patternthunderbird

Run a method everytime value changes


I wonder if any of you guys can help me with what I think is observing problem.

I have an element (svg to be more specific) that I want to update every time a value somewhere is changed.

I have variable: GetThreadTree().treeBoxObject.getFirstVisibleRow() that initially is 0. I want to run a function updateCanvas() every time value of GetThreadTree().treeBoxObject.getFirstVisibleRow() changes.

What I have is:

canvas.observe(GetThreadTree().treeBoxObject.getFirstVisibleRow(), "scroll", updateCanvas());

But it calls updateCanvas() only once, when it's called for the first time, and for some reason does not execute the code that is after it. I checked error console and nothing is there.

Any ideas?


Solution

  • One way of solving this problem is:

    var registeredRow = 0;
    
    function checkRow(){
        var row = GetThreadTree().treeBoxObject.getFirstVisibleRow();
        if (registeredRow != row) {
            registeredRow = row;
            updateCanvas();
        }
        window.setTimeout(checkRow, 1);
    }
    

    And before checkRow is called:

    registeredRow = GetThreadTree().treeBoxObject.getFirstVisibleRow();
    checkRow();
    

    But it's not the most elegant solution but it works ;)