func.apply( this, [ () => difference = ms ] );
let div=document.createElement('div');
let div_msg=document.createElement('div');
div_msg.className="msg_div";
div.append(div_msg);
let fuel=document.createElement('div');
fuel.className="fuel";
div.append(fuel);
let fuel_p=document.createElement('p');
fuel_p.className="fuel_p";
fuel_p.textContent='Расход топлива';
fuel.append(fuel_p);
let amount=document.createElement('div');
amount.className="amount";
fuel.append(amount);
let n1 =document.createElement('div');
n1.className="n1";
amount.append(n1);
let dot =document.createElement('div');
dot.className="dot";
amount.append(dot);
let n2 =document.createElement('div');
n2.className="n2";
amount.append(n2);
let btn = document.createElement('button');
btn.className="btn";
btn.textContent = "завести";
let btn2 = document.createElement('button');
btn2.className="btn2";
btn2.textContent = "заправить";
document.body.append(div,btn,btn2);
Car ={
make:'Lada',
model:'Kalina',
color:'blue',
fuel:0,
started:false,
start:function(){
if(Car.started===false && Car.fuel===0){
div_msg.innerHTML="";
div_msg.innerHTML="Бак пуст.Заправьте автомобиль";
}
if(Car.started===false && Car.fuel>0){
Car.started=true;
console.log('Car.started '+ Car.started);
div_msg.innerHTML="";
div_msg.innerHTML="Двигатель заведён.";
}
if(Car.started && Car.fuel<=0){
div_msg.innerHTML="";
div_msg.innerHTML="Двигатель заглох";
Car.started=false;
console.log('Car.started '+ Car.started);
}
},
addFuel:function(){
if(Car.fuel<=0){
Car.fuel=Car.fuel+10;
n1.textContent=`${Math.floor(Car.fuel/1000)}`;
dot.textContent=".";
n2.textContent=`${Math.floor(Car.fuel%1000)}`;
div_msg.innerHTML="";
div_msg.innerHTML="Автомобиль заправлен на 0.1л.";
console.log('Car.fuel=' + Car.fuel);
}else
this.classList.add('none_click');
},
rate1000: function(){
setInterval(()=>{
if(Car.started && Car.fuel<=0){
div_msg.innerHTML="";
div_msg.innerHTML="Двигатель заглох";
Car.started=false;
console.log('Car.started '+ Car.started);
}
if(Car.started && Car.fuel > 0){
Car.fuel--;
}
n1.textContent=`${Math.floor(Car.fuel/1000)}`;
dot.textContent=".";
n2.textContent=`${Math.floor(Car.fuel%1000)}`;
}, 1000);
},
};
Car.rate1000();
btn.addEventListener('click',Car.start);
btn2.addEventListener('click',Car.addFuel);
function waitForQQ( timeout = 5000, interval = 100 ) {
return new Promise( ( resolve, reject ) => {
check( resolve, reject, timeout )
} );
function check( res, rej, to ) {
if( to < 0 ) {
rej();
return;
}
if( typeof qq !== 'undefined' ) {// тут ещё подумать можно, как определить наличие
res( qq );
return;
}
setTimeout( check, interval, res, rej, to - interval );
}
}
waitForQQ().then(console.log)