いろんなウィジェットで使うし、
毎回同じ処理書いたりするのも無駄なので、
ここらで一旦まとめよーってことで。
あと、Google Syntax Highlighter for WordPressのテストも兼ねて。

/***************************************
 * my_httpRequest
 * Class: object
 * Description: a kind of wrapper for httpRequest
 * NOTE:
 */
function my_httpRequest(url, timeout_ms, func_success, func_fail)
{
    this.requestUrl = url;
    var request = new XMLHttpRequest();
    var timer = null;

    this.setRequestUrl = function(url_str) {
	this.requestUrl = url_str;
    };

    //	request send
    this.send = function() {
	request.onreadystatechange = function() {
	    if (request.readyState == 4) {
		clearTimeout(timer);
		timer = null;
		if (request.status == 200) {
		    if (func_success != null) {
			func_success(request);
		    }
		} else {
		    if (func_fail != null) {
			func_fail(request);
		    }
		}
	    }
	};
	request.open("GET", this.requestUrl, true);
	request.send();
	if (timeout_ms > 0) {
	    timer = setTimeout("timeout()", timeout_ms);
	}
    };

    //	request abort
    this.abort = function() {
	clearTimeout(timer);
	timer = null;
	request.abort();
    };

    //	request timeout
    timeout = function() {
	if (timer != null) {
	    clearTimeout(timer);
	    timer = null;
	    request.abort();

	    if (func_fail != null) {
		func_fail(request);
	    }
	}
    };

なんかインデントがダメダメだなー。

Posted in

2 responses to “httpRequest周りのあれこれ”

  1. Y田 Avatar
    Y田

    今ちょうど俺も公開実験用のHTMLのコードハイライター探してたとこだったんだけど、
    これすげー綺麗でいいね!

    でもSyntaxHighlighter Evolvedってやつの方が新しいかも。。右上にボタンが付いたり折り返し記号が付いたり。
    http://wordpress.org/extend/plugins/syntaxhighlighter/

    Like

  2. Naoharu Avatar
    Naoharu

    これかなりフォント&色が好みなんだよねぇ。
    WordPressこういうのがあるから好きだー。

    おぉ!ほんとだ!ナイス情報thx!<Evolved

    Like

Leave a reply to Naoharu Cancel reply