if( window.location.hash != pages) window.location.hash = pages;
$json = file_get_contents('php://input');
$obj = json_decode($json);
xmlhttp.onreadystatechange=function(){}
xmlhttp.send("id="+id+"&hash="+hash);
{"success":"true","data":{}}//ok
{"success:"false","error":{"code":1,"message":"bla"}//error
//success- избыточен, но есть на него идейные любители
function replaceWord(str,index,tag){
return str.substr(0,index)+str.substr(index).replace(/\w+/,"<"+tag+">$&</"+tag+">");
}
console.log(replaceWord("Hello World!",6,"strong"));
function replaceWord(str,index,tag){
return str.substr(0,index)+str.substr(index).replace(/[_0-9a-zA-Zа-яёА-ЯЁ]+/,"<"+tag+">$&</"+tag+">");
}
console.log(replaceWord("Привет Мир!!",7,"strong"));
console.log(replaceWord("Функция — это самый мощный инструмент для замены, какой только может быть.",14,"strong"));
//Привет <strong>Мир</strong>!!
//Функция — это <strong>самый</strong> мощный инструмент для замены, какой только может быть.
var likeArrayObject = {
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
length: 5
},
arr = Array.prototype.slice.call(likeArrayObject, 1, 4); //теперь это массив
console.log(likeArrayObject);
console.log(arr);
function getData(url, callback) {
var RESPONSES = {
'/countries': [
{name: 'Cameroon', continent: 'Africa'},
{name :'Fiji Islands', continent: 'Oceania'},
{name: 'Guatemala', continent: 'North America'},
{name: 'Japan', continent: 'Asia'},
{name: 'Yugoslavia', continent: 'Europe'},
{name: 'Tanzania', continent: 'Africa'}
],
'/cities': [
{name: 'Bamenda', country: 'Cameroon'},
{name: 'Suva', country: 'Fiji Islands'},
{name: 'Quetzaltenango', country: 'Guatemala'},
{name: 'Osaka', country: 'Japan'},
{name: 'Subotica', country: 'Yugoslavia'},
{name: 'Zanzibar', country: 'Tanzania'},
],
'/populations': [
{count: 138000, name: 'Bamenda'},
{count: 77366, name: 'Suva'},
{count: 90801, name: 'Quetzaltenango'},
{count: 2595674, name: 'Osaka'},
{count: 100386, name: 'Subotica'},
{count: 157634, name: 'Zanzibar'}
]
};
setTimeout(function () {
var result = RESPONSES[url];
if (!result) {
return callback('Unknown url');
}
callback(null, result);
}, Math.round(Math.random * 1000));
}
/**
* Ваши изменения ниже
*/
var requests = ['/countries', '/cities', '/populations'];
var responses = {}; //, counter=0;
for (var i = 0; i < 3; i++) {
var _request = requests[i];
var callback = function(request){
return function (error, result) {
responses[request] = result;
var i, j;
if (Object.keys(responses).length == 3) { //or counter++==3
var c = [], cc = [], p = 0;
for (i = 0; i < responses['/countries'].length; i++) {
if (responses['/countries'][i].continent === 'Africa') {
c.push(responses['/countries'][i].name);
}
}
for (i = 0; i < responses['/cities'].length; i++) {
for (j = 0; j < c.length; j++) {
if (responses['/cities'][i].country === c[j]) {
cc.push(responses['/cities'][i].name);
}
}
}
for (i = 0; i < responses['/populations'].length; i++) {
for (j = 0; j < cc.length; j++) {
if (responses['/populations'][i].name === cc[j]) {
p += responses['/populations'][i].count;
}
}
}
console.log('Total population in African cities: ' + p);
}
}
}(_request);
getData(_request, callback);
}
var requests = ['/countries', '/cities', '/populations'];
var responses = {};
function load(request){
return new Promise(function(resolve,reject){
getData(request,function(error, result){
if(!error){
resolve(result);
}else{
reject (error);
}
})
});
}
function getPopulation(state){
return Promise.all(requests.map(function(request){
return load(request);
})).then(function(data){
//console.log(arguments);
var countries=data[0],
cities=data[1],
populations=data[2],
result;
console.log(countries,cities,populations);
//calc here for state
return result;
},function(error){
console.warn("Data error",error);
});
}
getPopulation("Africa").then(console.log);
function loadScript(src,_timeout) {
return new Promise(function(resolve, reject){
if(!src){
reject(new TypeError("filename is missing"));
return;
}
var script=document.createElement("script"),
timer,
head=document.getElementsByTagName("head")[0];
head.appendChild(script);
function leanup(){
clearTimeout(timer);
timer=null;
script.onerror=script.onreadystatechange=script.onload=null;
}
function onload(){
leanup();
if(!script.onreadystatechange||(script.readyState&&script.readyState=="complete")){
resolve(script);
}
}
script.onerror=function(error){
leanup();
head.removeChild(script);
script=null;
reject(new Error("network"));
};
if (script.onreadystatechange === undefined) {
script.onload = onload;
} else {
script.onreadystatechange = onload;
}
timer=setTimeout(script.onerror,_timeout||30000);
script.setAttribute("type", "text/javascript");
script.setAttribute("src", src);
});
}
function loadCSS(src, _timeout) {
var css = document.createElement('link'), c = 1000;
document.getElementsByTagName('head')[0].appendChild(css);
css.setAttribute("rel", "stylesheet");
css.setAttribute("type", "text/css");
return new Promise(function(resolve, reject){
var c=(_timeout||10)*100;
if(src) {
css.onerror = function (error) {
if(timer)clearInterval(timer);
timer = null;
reject(new Error("network"));
};
var sheet, cssRules, timer;
if ('sheet' in css) {
sheet = 'sheet';
cssRules = 'cssRules';
}
else {
sheet = 'styleSheet';
cssRules = 'rules';
}
timer = setInterval(function(){
try {
if (css[sheet] && css[sheet][cssRules].length) {
clearInterval(timer);
timer = null;
resolve(css);
return;
}
}catch(e){}
if(c--<0){
clearInterval(timer);
timer = null;
reject(new Error("timeout"));
}
}, 10);
css.setAttribute("href", src);
}else{
reject(new TypeError("filename is missing"));
}
});
}
loadScript("/engine/classes/js/jquery.pickmeup.min.js").then(function(){
console.log("js ready");
},function(error){
console.log(error);
})
loadCSS("/engine/classes/css/pickmeup.min.css").then(function(){
console.log("css ready");
},function(error){
console.log(error);
})
var data = [
'08-10-2011',
'08-04-2007',
'03-11-2008',
'03-03-2008',
'03-14-2008',
'03-28-2005',
'03-20-2005'
];
function decodeDate(arr) {
var i, len = data.length,
obj = {},
date,
year,
month;
for (i = 0; i < len; i++) {
date=arr[i].split("-");
if(!obj[year=date[2]])obj[year]={};
if(!obj[year][month=date[0]]){
obj[year][month]=[date[1]];
}else{
obj[year][month].push(date[1]);
}
}
return obj;
}
console.log(decodeDate(data));