English 中文(简体)
Closure/scope JavaScript/jQuery
原标题:

I m trying to group some exisiting top-level functions inside a closure (to avoid polluting the global namespace) but I m not quite getting it to work.

First, all the JS works outside my anonymous function, but once I put it in the anonymous function I get an error of "crossfade is not defined". Does anyone see anything completely obvious that I am missing?

I m not quite getting why the the setInterval/crossfade works outside the anonymous function but not inside. Anything inside start() should be able to see vars/functions outside start() and it should all be protected in the closure created by the top-level anonymous function? I m not trying to access anything within crossfade(), I m just trying to execute it.

(function($) {

    //vars up here that internal functions can access
    //also using some jquery inside here, so using $

    function crossfade() {
        //body here
    }

    //other functions

    function start() {
        //body here

         cInterval = setInterval( crossfade() , 5000);
    } 

})(jQuery);
最佳回答

The setInterval method will be run in the scope of the window, so the crossfade function doesn t exist there. You have to make an anonymous function so that a closure is created that contains a reference to the function:

cInterval = window.setInterval(function() { crossfade(); }, 5000);
问题回答

Using setInterval( crossfade() , 5000); doesn t create a closure - it creates a string to be eval()d. You should use a function instead:

setInterval(function() { crossfade(); }, 5000);

When setInterval is passed a string, the string is evaluated in global scope. That explains why crossfade is not visible when setInterval fires.

setInterval can also be passed a function reference:

setInterval(crossfade, 5000);

in which case your code will work as expected, since crossfade is visible at the point where you call setInterval.

To avoid polluting the global scope, you can do a few things:

  • Extend jQuery, since you re already using jQuery. (Use jQuery as a namespace.)
  • Create a single object to hold your methods. (Create your own namespace.)




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签