Update only at new bar in indicator?

 
Why does the following not work in an indicator? The code following "if(prevtime == Time[0]) return(0);" gets executed each tick instead of each bar.

datetime prevtime=0;
...
if(prevtime == Time[0]) return(0);
prevtime = Time[0];
gets executed each tick;
but only want to update at each new bar;
...
 
Variable prevtime was initialized within the indicator start function and therefore reset at each tick.
 
use static memory
    static datetime prevtime=0;