Change img src by timer causes leak

L

lstipakov

The following code

<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript" src="../dojo-release-1.0.1/dojo/
dojo.js.uncompressed.js"
djConfig="parseOnLoad: true, isDebug: true, usePlainJson:
true"></script>

<script type="text/javascript">

var version = 1;

function onLoad()
{
setInterval(onTimer, 500)
}

function onTimer()
{
dojo.byId("img1").src = "../gettile.ashx?x=2&y=2&version="
+ version;

++ version;
}

dojo.addOnLoad(function() {debugger;onLoad();});
</script>
</head>
<body>
<img id="img1" src="../gettile.ashx?x=2&y=2&version=2" />
</body>
</html>


generates memory leak, about 1Mb per 6 secs in FF.

What I do wrong? Maybe it is not good to change img.src before prev
image has not been loaded yet? Or something else.

Thanks!
 
L

lstipakov

I've made code even simplier, still got leaks :(

<html>
<head>
<script type="text/javascript">
function onImageLoad()
{
setTimeout(onTimer, 100)
}
function onTimer()
{
document.getElementById("img1").src = "../bird.png?d=" +
Date.now();
document.getElementById("img1").onload =
onImageLoad;
}
</script>
</head>
<body onload="onImageLoad();">
<img id="img1" src="../bird.png" />
</body>
</html>
 
R

RobG

m00n said:
'onLoad' is an existing keyword. Maybe it conflicts there.

Please do not top-post here, reply below trimmed quotes.

The word "onload", however it is capitalised, is not a javascript
keyword. You may be confusing it with the onload property of the window
object that used to set a handler for the window load event.

As javascript is case sensitive, creating a global function called
"onLoad" will not clash with the window.onload property, although it is
not a good idea to have two such similar identifiers for global
variables. It can easily lead to confusion, as has occurred in this case.
 
M

m00n

RobG said:
Please do not top-post here, reply below trimmed quotes.

--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot

"T. Eliot" is not a coincidental palindrome.
I always have to go there when people try to teach me lessons.
 
P

Paul Lautman

m00n said:
"T. Eliot" is not a coincidental palindrome.
I always have to go there when people try to teach me lessons.

T.Eliot is not a palindrome at all! I guess your ignorance of that fact is
due to your stated unwillingness to learn.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
End of code. If you want a constant "timer", then use setInterval,
which is precisely for what you are doing.

But is setInterval(F, T) specified such that the N'th call of F occurs
as soon as possible after time NT after the initiating call, or is it
specified that the N'th call occurs asap after time T after the (N-1)th,
or is it left to the whim of the browser writer? Note that T is not
necessarily an integer multiple of the relevant system timer interval.

For an interval whose long-term average is correct, use setTimeout with
a delay calculated afresh each time to end at the right moment, the
accumulated error so far notwithstanding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top