간단한 차단식 AjaxTaskRunner 구현

1453 단어
/**
 * 
 */
TaskRunner = Base.extend({
                  
	intervalID : '',
	interval: 1000,//1 second
	running: false,
  
	runTask: function() {
		if(this.running === false) {
			this.running = true;
			this.run(this);
		}
	},
	run: function(){},
	start: function(config){
		if(config.run) {
			this.run = config.run;
		}
		if(config.interval) {
			this.interval = config.interval;
		}
		this.intervalID  = setInterval(this.runTask.bind(this), this.interval);
	},
	stop: function() {
		window.clearInterval(this.intervalID);
	}
});

/**
 *  
 */
function getAsynchronousUserNo(txtUserNo) {
    var runner = new TaskRunner();
    runner.start({
        run: function(task) {
            Server.sendRequest(
				"org.infinite.oa.port.adapter.ui.user.UserUI.getAsynchronousUserNo", 
                new DataCollection({"role": "manager"}), 
                function(response) {
                    if (response.Status == 1) {
                        if(response.get("userNo")) {
                            $(txtUserNo).value = response.get("userNo");
                            task.stop();//  , 
                        } else {
                            //  , 
                            task.running = false;
                        }
                    } else {
                        alert(response.Message);
                    }
				}
			);
		},
		interval: 500 
	});
}

좋은 웹페이지 즐겨찾기