Delayed Ajax update [redux]

I figured out that I could use the delayed Ajax update design pattern from yesterday to other things, so I made it possible to delay any function:
var T_TIMEOUTS = {};
function delayAjaxUpdate(id, interval, fn) {
    var time_out = T_TIMEOUTS[id];
    if(time_out)
        clearTimeout(time_out);
    T_TIMEOUTS[id] = setTimeout(fn, interval);
}

How I use it:

delayAjaxUpdate(li.json.id, 5000, $p(Chains.ajaxUpdate, li, json_data));

Beautiful :)

3 comments so far

Nice but should you really use the argument name interval for this though? I find it kind of confusing since you're not using setInterval here. :)

You're also polluting the global namespace with two new possible naming conflicts in this example. I'd probably have to put this code in a child object of my root namespace object before I'd be happy with it. Proper structuring is soo important in JS I think.

On a vaguely related note, http://humanized.com/weblog/20... has an interesting side effect of decreasing the number of ajex updates. It has the same possible side effect as this though, if the connection is lost/the browser crashes the changes are lost. Not so bad for deleting items, but quite a different story if you are adding things. You could stack them up for deletes and updates, but I wouldn't recommend it for adding information.

Simple and useful. Thanks!

Post a comment
Commenting on this post has expired.
© 2000-2009 amix. Powered by Skeletonz.