Loops on load

5 views
Skip to first unread message

Ed

unread,
Sep 4, 2008, 6:39:57 AM9/4/08
to TiddlyWiki
I tried my wiki with Google Chrome yesterday and found that it would
not load correctly. I traced the problem back to a function I have
running at the startup of the wiki. I stripped it down for testings
sake, and it still prevented the tiddlywiki from loading. Here are
the stripped down contents of the tiddler:

/***


!Code
***/

//{{{

for (var i=0;i<2;i++) {

}



//}}}

It is tagged systemConfig. My wiki has always worked in Firefox. I
then tried it in Internet Explorer and found that it also would not
load. Is there a reason why a for loop is preventing the wiki from
loading in its entirety?

Ed

FND

unread,
Sep 4, 2008, 9:33:06 AM9/4/08
to Tiddl...@googlegroups.com
> for (var i=0;i<2;i++) {
> }
> [...]
> Is there a reason why a for loop is preventing the wiki from
> loading in its entirety?

I don't see anything wrong with that - it should run two empty loops,
and that's it.
Can you upload a simple demo? Maybe there's something else interfering...


-- F.

Ed

unread,
Sep 4, 2008, 10:01:09 AM9/4/08
to TiddlyWiki
Go to http://thefamilynet.googlepages.com/Blog.html

It should load. If you search for "Resetr", you should find a tiddler
with the code commented out.

Go to http://thefamilynet.googlepages.com/Blog2.html

This is the same page with the for loop active. It should work in
Firefox, but does not load for me in IE (and Chrome).

Thoughts?

Eric Shulman

unread,
Sep 4, 2008, 10:55:10 AM9/4/08
to TiddlyWiki
> I tried my wiki with Google Chrome yesterday and found that it would
> not load correctly.  I traced the problem back to a function I have
> running at the startup of the wiki.
> //{{{
>                 for (var i=0;i<2;i++) {
>                 }
> //}}}
> Is there a reason why a for loop is preventing the wiki from
> loading in its entirety?

During startup plugins are invoked by the core from a function called
loadPlugins(). This function performs several loops through an alpha-
sorted list of 'systemConfig' tiddlers (i.e., plugins) in order to
invoke each one in turn. The index variable used to control these
loops is, notably, named "i"...

While the first loop in this function is written using a *locally-
scoped* 'var i...' declaration,
for(var i=0; i<nPlugins; i++)
the index variable for two other loops in the loadPlugins() code seem
to have been inadvertently declared with an implicit *global-scope* by
omitting the 'var' from the syntax, like this:
for(i=0; i<nPlugins; i++)
for(i=0; i<toLoad.length; i++)

Unfortunately, in your errant plugin code, you also perform a loop
using the index variable, "i"... for which you *do* use the locally-
scoped 'var' declaration.

However, there are some subtle browser-specific differences in the
javascript variable scoping rules. In IE, it seems that, despite the
use of 'var i' in your code, the loop index is, in fact, globally-
scoped, while in FireFox and other browsers, the variable is correctly
handled with local scoping.

As a result, when your plugin code is loaded, it immediately alters
the index of the outer loop that controls the loading of plugins,
causing that loop to, in effect, jump back to almost the beginning of
the list of plugins and repeat the loading process again. Of course,
as soon as it reloads your plugin, the loop index is 'stepped on'
again, and the cycle repeats, infinitely.

The proper way to fix this is to correct the core code so that all the
loop indices are declared with local scoping, so that plugins can't
step on those values during load-time processing.

In the mean time, one workaround is to wrap the plugin's
initialization code within a function declaration and then immediately
invoke that function. This ensures that the scope of any variables
declared in that code is isolated within a locally-scoped function.
Something like this:

function myPluginInit() {
for (var i=0; ..... ) {
...
}
}
myPluginInit();

I've opened a ticket for this problem (see http://trac.tiddlywiki.org/ticket/754),
so it should hopefully go away in the next release of the TW core...

-e
Eric Shulman
TiddlyTools / ELS Design Studios

Ed

unread,
Sep 4, 2008, 2:20:44 PM9/4/08
to TiddlyWiki
Excellent. Thanks again, everyone!

Ed
> I've opened a ticket for this problem (seehttp://trac.tiddlywiki.org/ticket/754),
Reply all
Reply to author
Forward
0 new messages