é¢æ°ãéåæåããé¢æ°ãä½ã£ã¦ã¿ãã
Function.prototype.asynchronize = function() { var f = this; return function() { var resultcb = function() {}; var failcb = function(e) { throw e }; if (typeof arguments[arguments.length - 1] == 'function') { resultcb = Array.prototype.pop.apply(arguments); if (typeof arguments[arguments.length - 1] == 'function') { failcb = Array.prototype.pop.apply(arguments); } } try { var result = f.apply(this, arguments); setTimeout(function() { resultcb(result) }, 0); } catch (e) { setTimeout(function() { failcb(e) }, 0); } }; };
ä½ãå¬ããã®ãï¼
ãã¨ãã°ã以ä¸ã®ããã«ãã¼ã¿ã cookie ã«ä¿åããå ´åããã£ã¦
function putData(data) { data = escape(data); if (data.length > 5000) throw Error('ãã¼ã¿ã大ãããããï¼ï¼'); document.cookie = 'data=' + data; return 'ok'; }
ãã使ãã¨ãã
var result = putData(); if (result == 'ok') { ... }
ããããéä¸ã§ãã¼ã¿ããµã¼ãã«ä¿åããããã«å¤æ´ãããã¨ãã¨ãã
éåæå¦çã«ãããã¦å°ãï¼ï¼
var reuslt = putData(); // éåæãªã®ã§ if (result == 'ok') { // çµæããå ã«å¦çãå ã«æµãã¦ãã¾ãã .... }
ãªã®ã§ãå°æ¥ãµã¼ãã«ä»»ããããç¥ããªãå¦çã¨ãã¯ãéåæåãã¦ããã¨ä¾¿å©ãããããªãã
ãããªæã
putData.asynchronize()(function(result) { if (result == 'ok') { .... } });
éãåºæ¥ãã便å©ã ã¨æãã®ã ã
æ®å¿µãªãããJavaScript ã«ã¯éåæã¡ãã»ã¼ã¸ãåæåããæ¹æ³ã¯ãªãï¼æ®å¿µï¼ï¼ï¼ã»ãã¨ã«æ®å¿µï¼