02.04.2019, 17:25. Показов 973. Ответов 1
Как это работает?
почему, запуская множество setTimeout, внутри setInterval,
фиксируя остановку SetTimeout с помощью if, а setInterval с помощью clearInterval - сбивается время и очередность?
В контексте данной ситуации, Я не могу вЪехать: как может существовать одновременно ОДНОПОТОЧНОСТЬ И АСИНХРОННОСТЬ.
Я понимаю: АСИНХРОННОСТЬ - onclick, то есть event из документа непосредственно
ОДНОПОТОЧНОСТЬ: - стек, собирающийся из последовательного выполнения.
Ну а теперь прибавим к этому setTimeout и clearTimeout, получим =
ОДНОПОТОЧНОСТЬ ИДЕТ ЛЕСОМ, А
АСИНХРОННОСТЬ В ПРИПАДКЕ ЭПИЛЕПСИИИ, ничего не работает, ибо, конечно же, мое слабое образование дает о себе знать, а не парадигма JS ломается об удары бытия.
Суть нижеследующего кода в том, что он выполняет смену картинок, где:
РЕЖИМ А - смена картинок последовательная
РЕЖИМ Б - затухание и появление картинок
РЕЖИМ Ц - случайная очередность появления картинок
Выполняет его верно ровно до того момента, как начинаешь переключать кнопки - сбивает очередность и скорость выполнения. Не обязательно вам пытаться разобраться в этом коде, достаточно ничего не делать. Спасибо за внимание! =)
ПРИМЕР минимально(задать несколько подобных запросов и ничего не будет работать):
| JavaScript |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| setInterval(function(){
if(b==1){
setTimeout(lay2b(), 2000);
b++;
}
else if(b==2){
setTimeout(lay3b(), 2000);
b++;
}
else if(b==3){
setTimeout(lay4b(), 2000);
b++;
}
else if(b==4 ){
setTimeout(lay1b(), 2000);
b = b - 3;
}}, 2000);} |
|
МАКСИМАЛЬНО НЕРАБОЧИЙ КОД, где все правильно, но только не выполнение этого кода:
| JavaScript |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
| var rregime_A;
var A_ON = false; /*режим А вкл/выкл */
var a = 1; /*переменная в Режиме А*/
var c = 1;
var b = 1; /*переменная в Режиме B*/
var a1 =false;
var a2 =false;
var a3 =false;
var a4 =false;
window.event = pageLoadedHandler();
function pageLoadedHandler() {
alert("event!");
}
rregime_A = function regime_A(){
document.querySelector('#lay').style.zIndex = 2 ;
document.querySelector('#lay').style.trnsition = "1s" ;
B_ON = false;
C_ON = false;
clearInterval(rregime_B);
if(b==1){b1=true}
else if(b==2){b2=true}
else if(b==3){b3=true}
else if(b==4){b4=true}
b = 5;
clearInterval(rregime_C);
if(c==1){c1=true}
else if(c==2){c2=true}
else if(c==3){c3=true}
else if(c==4){c4=true}
c = 5;
lay1();
if(a1==true){a=1;}
else if(a2==true){a=2;}
else if(a3==true){a=3;}
else if(a4==true){a=4;}
document.querySelector("#lay").style.opacity = 1 ;
if(A_ON == false){
A_ON = false;
setInterval(function(){
if(a==1){
setTimeout(lay2(), 1000);
a++;
}
else if(a==2){
setTimeout(lay3(), 1000);
a++;
}
else if(a==3){
setTimeout(lay4(), 1000);
a++;
}
else if(a==4){
setTimeout(lay1(), 1000);
a = a - 3;
}}, 1000);}
a1=false;a2=false;a3=false;a4=false;
}
var rregime_B;
var B_ON = false; /*режим B вкл/выкл */
var b1 =false;
var b2 =false;
var b3 =false;
var b4 =false;
rregime_B = function regime_B(){
document.querySelector('#lay').style.zIndex = 2 ;
document.querySelector('#lay').style.trnsition = "1s" ;
A_ON = false;
C_ON = false;
clearInterval(rregime_A);
if(a==1){a1=true}
else if(a==2){a2=true}
else if(a==3){a3=true}
else if(a==4){a4=true}
a = 5;
clearInterval(rregime_C);
if(c==1){c1=true}
else if(c==2){c2=true}
else if(c==3){c3=true}
else if(c==4){c4=true}
c = 5;
lay1();
if(b1==true){b=1;}
else if(b2==true){b=2;}
else if(b3==true){b=3;}
else if(b4==true){b=4;}
if(B_ON == false){
B_ON = false;
setInterval(function(){
if(b==1){
setTimeout(lay2b(), 2000);
b++;
}
else if(b==2){
setTimeout(lay3b(), 2000);
b++;
}
else if(b==3){
setTimeout(lay4b(), 2000);
b++;
}
else if(b==4 ){
setTimeout(lay1b(), 2000);
b = b - 3;
}}, 2000);}
b1=false;b2=false;b3=false;b4=false;
}
var rregime_C;
var C_ON = false;
var c1 = false;
var c2 = false;
var c3 = false;
var c4 = false;
rregime_C = function regime_C(){
document.querySelector('#lay').style.trnsition = "1s" ;
document.querySelector('#lay_in').style.trnsition = "1s" ;
document.querySelector('#lay').style.zIndex = -55 ;
B_ON = false;
A_ON = false;
/*clearInterval(rregime_B);
if(b==1){b1=true}
else if(b==2){b2=true}
else if(b==3){b3=true}
else if(b==4){b4=true}
b = 5;
clearInterval(rregime_A);
if(a==1){a1=true}
else if(a==2){a2=true}
else if(a==3){a3=true}
else if(a==4){a4=true}
a = 5;
lay1();
if(c1==true){c=1;}
else if(c2==true){c=2;}
else if(c3==true){c=3;}
else if(c4==true){c=4;}
if(C_ON == false){
C_ON = true;*/
setInterval(function(){
var accidental = random(5,1);
if(1==accidental && c != 5){
setTimeout(lay1cc(), 1000);
var accidental = random(5,1);
}
else if(2==accidental && c != 5){
setTimeout(lay2cc(), 1000);
var accidental = random(5,1);
}
else if(3==accidental && c != 5){
setTimeout(lay3cc(), 1000);
var accidental = random(5,1);
}
else if(4==accidental && c != 5){
setTimeout(lay4cc(), 1000);
}}, 1000);}
c1=false;c2=false;c3=false;c4=false;
/*}*/
function random(max,min){
return Math.floor(Math.random() * (max - min) + min);
}
function lay1 () {
document.querySelector("#lay").style.background = 'url(lay1.png)100% 100% no-repeat' ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;
}
function lay2() {
document.querySelector('#lay').style.background = "url(lay2.png)100% 100% no-repeat" ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;}
function lay3() {
document.querySelector('#lay').style.background = "url(lay3.png)100% 100% no-repeat" ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;}
function lay4() {
document.querySelector('#lay').style.background = "url(lay4.png)100% 100% no-repeat" ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;}
var opaci1b = 0;
var opaci2b = 0;
var opaci3b = 0;
var opaci4b = 0;
var opa1;
var opa2;
var opa3;
var opa4;
function lay1b () {
document.querySelector("#lay").style.background = 'url(lay1.png)100% 100% no-repeat' ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;
if(opaci1b == 0){
opa1 = 0.1; opaci1b++;}else{opa1 = 1; opaci1b--;}
document.querySelector("#lay").style.opacity = opa1 ;
}
function lay2b() {
document.querySelector('#lay').style.background = "url(lay2.png)100% 100% no-repeat" ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;
if(opaci2b == 0){
opa2 = 1; opaci2b++;}else{opa2 = 0.1; opaci2b--;}
document.querySelector("#lay").style.opacity = opa2 ;
}
function lay3b() {
document.querySelector('#lay').style.background = "url(lay3.png)100% 100% no-repeat" ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;
if(opaci3b == 0){
opa3 = 0.1; opaci3b++;}else{opa3 = 1; opaci3b--;}
document.querySelector("#lay").style.opacity = opa3 ;}
function lay4b() {
document.querySelector('#lay').style.background = "url(lay4.png)100% 100% no-repeat" ;
document.querySelector("#lay").style. backgroundSize = 'cover' ;
if(opaci4b == 0){
opa4 = 1; opaci4b++;}else{opa4 = 0.1; opaci4b--;}
document.querySelector("#lay").style.opacity = opa4 ;}
function lay1cc () {
document.querySelector("#lay_in").style.background = 'url(lay1.png)100% 100% no-repeat' ;
document.querySelector("#lay_in").style. backgroundSize = 'cover' ;
document.querySelector('#lay_in').style.zIndex = 1 ;
document.querySelector('#lay').style.zIndex = -1 ;
}
function lay2cc() {
document.querySelector('#lay_in').style.background = "url(lay2.png)100% 100% no-repeat" ;
document.querySelector("#lay_in").style. backgroundSize = 'cover' ;
document.querySelector('#lay_in').style.zIndex = 1 ;
document.querySelector('#lay').style.zIndex = -1 ;}
function lay3cc() {
document.querySelector('#lay_in').style.background = "url(lay3.png)100% 100% no-repeat" ;
document.querySelector("#lay_in").style. backgroundSize = 'cover' ;
document.querySelector('#lay_in').style.zIndex = 1 ;
document.querySelector('#lay').style.zIndex = -1 ;}
function lay4cc() {
document.querySelector('#lay_in').style.background = "url(lay4.png)100% 100% no-repeat" ;
document.querySelector("#lay_in").style. backgroundSize = 'cover' ;
document.querySelector('#lay_in').style.zIndex = 1 ;
document.querySelector('#lay').style.zIndex = -1 ;} |
|
| HTML5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
| <!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title></title>
<script src="script.js" ></script>
</head>
<body>
<style type="text/css">
body{background-color: black; padding: 0px; margin: 0px; color: white;}
#lay {
position: absolute; top:0px;
display: block;
height: 900px; /* Высота блока */
width: 100%;
border: 2px solid #000; /* Параметры рамки */
background: url(lay1.png)100% 100% no-repeat ;/* Добавляем фон */
background-size: cover; /* Масштабируем фон */
transition: 1s linear;
} #lay_in {
position: absolute; top:0px;
display: block;
height: 900px; /* Высота блока */
width: 100%;
border: 2px solid #000; /* Параметры рамки */
background: url(лес2.png)100% 100% no-repeat ;/* Добавляем фон */
background-size: cover; /* Масштабируем фон */
transition: 1s linear;
z-index: -44;
}
#extent{
position: relative; top:0px;
height: 905px;
width: 100%;
}
#falling_man{
grid-area: falling_man;
position: relative;
width: 334px;
border:1px solid orange;
top:0px;
padding: 4px 4px 1px 4px;
margin: 5px 5px 5px 5px;
background-color: #282923;
}
img#falling_man_img{
display: inline-block; /*img свойственно Inline поведение*/
position: relative;
}
.text_f_m{
display: inline-block;
position: absolute; /*absolute действует внутри div*/
margin-left: 3px;
color: orange;
}
.button{
font-size: 45%;
margin-top:4px;
}
</style>
<div id="lay"></div>
<div id="lay_in"></div>
<div id="extent"></div>
<div id="falling_man">
<img id="falling_man_img" src="falling-man.jpg" ><div class="text_f_m">Анимация "Гирлянда"<br>Панель управления: <div class="button"><button onclick="rregime_A()">Режим "A"</button><button onclick="rregime_B()">Режим "B"</button><button onclick="rregime_C()">Режим "C"</button></div>
</div>
</div>
</body>
</html> |
|