Javascript, wait for something to be true, and then run the action

Well the kindof name says what I need. Since in Javascript-asynchronous time I have to know when something becomes true. I do not want busy.

Through

function do_when(predicate, action, timeout_step) {
    if (predicate()) {
        action();
    } else {
        setTimeout(do_when, timeout_step, predicate, action, timeout_step);
    }
}

Is this good javascript or can I do better?

+5
source share
3 answers

This is pretty decent, if it's easy enough to read, and it works great, then it's generally good javascript.

Effectively, it is usually best to call the function whenever true is set. Therefore, in any function executed for predicate()return true, you can simply call action()at the end. But I'm sure you would do it if you could, right?

, javascript , , , ​​ .

0

, , . JavaScript . , , , :

var observable = createObservable({ propToWatch: false });
observable.observe('propToWatch', function (oldValue, newValue) { 
    alert('propToWatch has changed from ' + oldValue + ' to ' + newValue); 
});
observable.propToWatch(true); // alert pops

, . (n.b. ), , :

var createMediator = function () {
    var events = {};
    return {
        subscribe: function (eventName, callback) {
            events[eventName] = events[eventName] || [];
            events[eventName].push(callback);
        },
        publish: function (eventName) {
            var i, callbacks = events[eventName], args;
            if (callbacks) {
                args = Array.prototype.slice.call(arguments, 1);
                for (i = 0; i < callbacks.length; i++) {
                    callbacks[i].apply(null, args);
                }
            }
        }
    };
};

var createObservable = function (properties) {
    var notifier = createMediator(), createObservableProperty, observable;
    createObservableProperty = function (propName, value) {
        return function (newValue) {
            var oldValue;
            if (typeof newValue !== 'undefined' &&
                value !== newValue) {
                oldValue = value;
                value = newValue;
                notifier.publish(propName, oldValue, value);
            }
            return value;
        };
    };
    observable = {
        register: function (propName, value) {
            this[propName] = createObservableProperty(propName, value);
            this.observableProperties.push(propName);
        },
        observe: function (propName, observer) {
            notifier.subscribe(propName, observer);
        },
        observableProperties: []
    };
    for (propName in properties) {
        observable.register(propName, properties[propName]);
    }
    return observable;
};

( createMediator), . ( , jQuery, . D'oh!) , , , . !

+6

, :

, " ", 2.

function observable (value, condition, callback){
    this.value = value;
    this.condition = condition;
    this.callback = callback;
}

observable.prototype = {
    get value () {
        return this._value;
    },
    set value (value) {
        this._value = value;
        if (this.condition && this.callback && this.condition (value)) {
            this.callback (value);
        }
    }
};

condition = function (value) {
    console.log ('condition', value);
    return value === 2;
}

callback = function (value) {
    console.info ('Big Brother is watching you!');
}

var a = new observable (0, condition, callback);

console.log ('set value to 1');
a.value = 1;
console.log ('set value to 2');
a.value = 2;
console.log ('set value to 3');
a.value = 3;

firefox

-1
source

All Articles