mtrpcic.net Writing software since 1932

22Aug/110

Introducing PollJS – Javascript timers made easy.

One thing that has always caused issue when writing web applications is the issue of querying the server for long-running background tasks (generating thumbnails or PDF's, sending batch emails, etc).  Often times these actions are too long-running to be contained in an HTTP request, so a short request is made and an interval is set up to begin querying for the eventual finish.  This isn't a terrible way to do things, but the current implementation was lacking.  For example, to kill the interval you need to assign the returned value of your "setInterval" call to a variable, and clear it out with "clearInterval".  This raises the problem of variable scope.  Now you need to go up and up and up, and declare a global variable to contain this interval, simply so you can kill it later.

But what if that long running job never gets executed during this particular session?  And what if you have many different operations that need this type of implementation?  You've now got your global namespace polluted with oft-unused variables.  That's where PollJS steps in.

PollJS is a convenience wrapper for the native "setTimeout", "setInterval", "clearTimeout", and "clearInterval" methods.  It wraps them with a simple definition method, and extends their functionality with additional options:

// A Basic Example
Poll.start({
    "update_users",
    interval: 1500,
    action: function(){
        alert("Updated!");
    }
});

Poll.stop("update_users");

You can read more about the project, and download the source code on the Github page.

11Aug/110

Javascript Variable History

I recently answered a Stackoverflow Question where a user wanted to know if there was a way, in JavaScript, to check if a variable has ever been a particular value.  Of course, there is no default built-in way to do this, but I provided (what I think to be) a valid answer.  However, after a few minutes, I looked at it and thought that it might be able to help other people as well.  So here it is - Chronicle.  Chronicle lets you define a special type of object, and you can check the history of that object at any point in time.  Here's a quick example:

var isValid = new Chronicle(false); // Default the value to false

isValid.set("not valid"); // Set it to a string
isValid.set(true); // Set it to true

// This will return true if the value inside this Chronicle
// has ever been true, and return false otherwise.
isValid.was(false);

If anyone wants this functionality on your site, it's as easy as including/running the following javascript anywhere before you need to use a Chronicle (as long as it's within the proper scope, of course). Chronicle works in any scope, in any situation.

function Chronicle(val){
    this.value = val;
    this.history = [];
    if(val != undefined){
        this.history.push(val);
    }
}

Chronicle.prototype.set = function(val){
    this.value = val;
    this.history.push(val);
};

Chronicle.prototype.was = function(val){
    return this.history.indexOf(val) >= 0;
};

Chronicle.prototype.toString = function(){
    return this.value.toString();
}