본문 바로가기

Web/Javascript

queue

[code]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">

<script type="text/javascript">
var callbackType = function() {
    this.index        = -1;
    this.MAX_SIZE    = 100;
    this.INDEX        = 0;
    this.SUCCESS    = 1;
    this.ERROR        = 2;
    this.queue        = [];

    this.add = function(successFunc, errorFunc) {
        var index = this.incrementIndex();
        this.queue[index] = [index, successFunc, errorFunc];
    }

    this.incrementIndex = function() {
        if (++this.index >= this.MAX_SIZE) {
            this.index = 0;
        }

        return this.index;
    }

    this.remove = function(key) {
        for (i in this.queue) {
            if (this.queue[i][0] == key) {
                this.queue.splice(i, 1);
            }
        }
    }

    this.print = function() {
        for (var i in this.queue) {
            this.queue[i][1](this.queue[i][0]);
            this.queue[i][2](this.queue[i][0]);
        }
    }

    this.count = function() {
        return this.queue.length;
    }
}

var gCallbackList = new callbackType;

function setCallbackFunc(name, successCallback, errorCallback) {
    gCallbackList.add(successCallback, errorCallback);
}

function showCallbackList() {
    document.getElementById("count").innerText = "count : " + gCallbackList.count;
}

function showCallbackDetailList() {
    gCallbackList.print();
}

function success(no) {
    document.getElementById('success').innerHTML += "success " + no + ", ";
}

function error(no) {
    document.getElementById('success').innerHTML += "error " + no + "<br/>";
}

function input() {
    setCallbackFunc("test", success, error);
}

function remove() {
    var no = document.getElementById("remove").value;
    gCallbackList.remove(no);
}


window.onload = function() {
    for (var i=0; i<10; ++i) {
        input();
    }
    showCallbackDetailList();
}
</script>


</head>
<button onclick="input()">input</button>
<button onclick="showCallbackList()">show list</button>
<button onclick="showCallbackDetailList()">show detail list</button>
<button onclick="remove()">remove</button><input type="text" value="2" id="remove" />
<body>
<div id="index"></div><hr>
<div id="count"></div><hr>
<div id="success"></div><hr>
<div id="error"></div>
</body>
</html>
[/code]

'Web > Javascript' 카테고리의 다른 글

inheritance  (0) 2013.09.26
string to json  (0) 2013.09.26
remove array  (0) 2013.09.26
callback  (0) 2013.09.26
addEventListener  (0) 2013.09.26