www.webdeveloper.com
Results 1 to 4 of 4

Thread: Problem with setTimeout

  1. #1
    Join Date
    Nov 2009
    Posts
    109

    Problem with setTimeout

    Hi!

    I am reading a javascript software where into the code there is this thing:

    Code:
    // -- Start Comment
    // to make setTimeout parameters compatible with IE, tip taken from the Stchur Talks website.
    // URL: http://ecmascript.stchur.com/2006/06/07/settimeout-revisited/
    
    if (!window.sstchur) { window.sstchur = {}; }
    if (!sstchur.web) { sstchur.web = {}; }
    if (!sstchur.web.js) { sstchur.web.js = {}; }
    
    sstchur.web.js.xb =
    {
      setTimeout: function(fnPointer, ms)
      {
        var args = arguments;
        function proxy()
        {
          var params = new Array();
          var i;
          for (i = 2; i < args.length; i++)
            { params.push(args[i]); }
    
          fnPointer.apply(this, params);
        }
    
        return window.setTimeout(proxy, ms);
      }
    };
    // -- End Comment
    and I would like to understand what do it does. Unfortunately I don't know the problem of setTimeout on IE and I don't understand these things:
    1) Why the writer used many strange variable like these:
    sstchur, web, js, xb
    ? What is the meaning of:
    Code:
    if (!window.sstchur) { window.sstchur = {}; }
    if (!sstchur.web) { sstchur.web = {}; }
    if (!sstchur.web.js) { sstchur.web.js = {}; }
    sstchur.web.js.xb =
    ?
    2) How you can traslate this code for me?
    if (!something) { something = {}; }
    3) Why the writer want to remove from input of setTimeout first and second fariable with function proxy?
    Thanks

  2. #2
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    152
    if (!window.sstchur) { window.sstchur = {}; }
    if (!sstchur.web) { sstchur.web = {}; }
    if (!sstchur.web.js) { sstchur.web.js = {}; }
    sstchur.web.js.xb =
    it's just defining a custom namespace for the setimeout method. The checks are there to make sure that the names aren't already taken. If sstchur.web.js.xb already exists, he doesn't want to overwrite it, but just add to it. Something like that.

    I'm sure it could be put more nicely into a namespace function.

    He mentions about not wanting to use a loop on params in his article, and just from what I'm learning he could eliminate

    Code:
    var params = new Array();
          var i;
          for (i = 2; i < args.length; i++)
            { params.push(args[i]); }
    and instead use. ( I might be wrong)
    Code:
    var params = Array.prototype.slice.call(args,2);
    Basically the reason for creating this code is to mimic/copy mozilla's timeout in IE. I didn't know this, but from what he's saying in mozilla you can pass a third argument in settimeout, which contains parameters to pass to the function like an object for instance.

    I'm only learning this stuff myself, so maybe someone else can give you a better breakdown.

    RPG

  3. #3
    Join Date
    Nov 2009
    Posts
    109
    Therefore if I write "if (!something) { something = {}; }" Do I want to write this thing? :
    "If something is not defined then create something and add to something variable nothing"

    Thanks very much rpg2009! I like this forum more and more! I hope to have others helps from this forum!

  4. #4
    Join Date
    Dec 2009
    Location
    Shropshire UK
    Posts
    152
    Look as I say no expert, but I don't think it's necessary for you to follow his naming convention.

    I'm sure var myTools = { code here } or something like that would suffice, rather than sstchur.web.js.xb = { code here }

    However if required personally I'd want to maybe use a namespace function to do this. Would be a bit more flexible.

    Here's one I've just written, not sure if it can be improved on.

    PHP Code:
    function namespace(newName){
        var 
    names newName.split('.'), obj windowchildObj;
        while (
    names.length){
            
    childObj names.shift();
            if (!
    obj[childObj]){ obj[childObj] = {}; }
            
    obj obj[childObj];
        }
    }
    obj = {name:'Bob'age36}

    namespace (
    'obj.sub1.sub2.sub3'); // create namespace
    alert (obj.name); // Bob - Note obj isn't written over
    alert (typeof obj.sub1.sub2.sub3); // object - but the rest are added 
    RPG

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles