How to rewrite functions to perform reset and stop with one button click
我尝试过很多排列,但在一个有效的排列上无法实现…
这是一个学习/演示页面的简短脚本,它可以在您的本地主机上运行,任何想看到它行为不正常的人都可以。双击"停止"按钮可以使其正常工作,但似乎必须有一种方法可以同时停止循环和重置,只需单击一下。
我只学习了几个星期的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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | MY Loop // At window loading, be good to have some .className // set on the DIV elements which require them, so they show up. var ScaryMonster = window.onload=function portofOrigin(){ Div1ne.className ="AsBeforeBubba"; Div2wo.className ="AsBeforeBubba"; Div3hree.className ="AsBeforeBubba"; Div4our.className ="AsBeforeBubba"; Div5iver.className ="AsBeforeBubba"; }; // Then go ahead and define a shwack of functions the DIVS will // be manipulated by through the action of the LOOP code. // functions: ItemBar1 function ItemBar1COLOUR(){ timeoutIDxDiv1 = window.setTimeout(Div1neDOsomethingDoggoneIt, 160); } function Div1neDOsomethingDoggoneIt(){ Div1ne.className ="springME"; Div2wo.className ="defaultBORED"; Div3hree.className ="defaultBORED"; Div4our.className ="defaultBORED"; Div5iver.className ="defaultBORED"; } // functions: ItemBar2 function ItemBar2COLOUR(){ timeoutIDxDiv2 = window.setTimeout(Div2woDOsomethingDoggoneIt, 320); } function Div2woDOsomethingDoggoneIt(){ Div1ne.className ="defaultBORED"; Div2wo.className ="springME"; Div3hree.className ="defaultBORED"; Div4our.className ="defaultBORED"; Div5iver.className ="defaultBORED"; } // functions: ItemBar3 // here the Timer Function is defined for the Item #3 of the set of the Loop function ItemBar3COLOUR(){ // it is set to fire, if called, at 480/800 milliseconds timeoutIDxDiv3 = window.setTimeout(Div3hreeDOsomethingDoggoneIt, 480); } // This, is the Function which contains the code which // will be run if Item #3 is called. function Div3hreeDOsomethingDoggoneIt(){ // .defaultBORED is the *non~lit~up* state of appearance // of the divs while the loop is running. Div1ne.className ="defaultBORED"; Div2wo.className ="defaultBORED"; // while .springME is the *active* display appearance // of the div during"animation loop* Div3hree.className ="springME"; Div4our.className ="defaultBORED"; Div5iver.className ="defaultBORED"; } // functions: ItemBar4 function ItemBar4COLOUR(){ timeoutIDxDiv4 = window.setTimeout(Div4ourDOsomethingDoggoneIt, 640); } function Div4ourDOsomethingDoggoneIt(){ Div1ne.className ="defaultBORED"; Div2wo.className ="defaultBORED"; Div3hree.className ="defaultBORED"; Div4our.className ="springME"; Div5iver.className ="defaultBORED"; } // functions: ItemBar5 function ItemBar5COLOUR(){ timeoutIDxDiv5 = window.setTimeout(Div5iveDOsomethingDoggoneIt, 800); } function Div5iveDOsomethingDoggoneIt(){ Div1ne.className ="defaultBORED"; Div2wo.className ="defaultBORED"; Div3hree.className ="defaultBORED"; Div4our.className ="defaultBORED"; Div5iver.className ="springME"; } /* ****************************************** */ // This is the BEGIN POINT of the Main Timer Loop To create the faux~animation function SetThemUP(){ // This Function creates a TIMESTAMP anywhere you wish to // place one, by calling the function. function JustTIMEstamp(){ var OkayFIRE = +new Date(); return OkayFIRE; } // For Whatever reason this works as a holder for a whole lot of functions function wellrelaxCODEworkWITHmeHere(){ // The first item in this Function collection, is a TimeStamp. // Obtained by calling a Function which has code to make one. var newOkayFIRE = JustTIMEstamp(); // 1 of 4 Functions needed to build a friggin' <br />. // It is a Function which when called, appends the // assembled LB to the DOM as a child of the target DIV element. function SoWriteTimeStampToDivYello(){ var myDiv1 = document.getElementById("Aaaiyeeah"); var YellowTitleWrite = myDiv1.appendChild(document.createTextNode(newOkayFIRE)); return YellowTitleWrite; } // 2 of 4. sic. it needs to create a discrete html element... // the one referrenced above. function FancyShitForLBWIN1(){ var compLbr="var inTheLinebreaks"; var thirdcompOFintheLinebreaks ="=document.createElement('br');"; var AssembledDocCreateLB=(compLbr+ thirdcompOFintheLinebreaks); return AssembledDocCreateLB; } // 3 of 4. sic. and surprisingly, only works by directly // inserting a line break onto the variable being // cobbled together to provide a recycleable <br /> construct... function middleware(){ var myDiv33 = document.getElementById("Aaaiyeeah"); var inTheLinebreaks7=document.createElement('br'); myDiv33.appendChild(inTheLinebreaks7); } // 4 of 4 and the final component for the asset //"reuseable <br />" which can be deployed in DIV"Aaaiyeeah" ONLY // but of course, as many times as required, so at least it is useful. function FancyShitForLBWIN2wo(){ var theAppending1stPart="myDiv42.appendChild("; var varNameProper=("inTheLinebreaks"); var fouthPart=");"; var assembleAppenderHerself=(theAppending1stPart + varNameProper + fouthPart); return assembleAppenderHerself; } // Oh, LookitThet, here it is, getting called. // So, this is inside the wrapper Function, inside the TIMER Loop. // Which has the effect that each time the LOOP gets called, // a record of the TIMESTAMP GO~Time is written out to display. SoWriteTimeStampToDivYello(); FancyShitForLBWIN1();middleware();FancyShitForLBWIN2wo(); //LINEBREAK // wellrelaxCODEworkWITHmeHere() end of function: } // then call all of em at one shot here: wellrelaxCODEworkWITHmeHere(); // these are the calling of the 5ive functions which are defined OUTSIDE of the MainTimerLoop; the STEPS or Components, // from which the LOOP is assembled. ItemBar1COLOUR(); ItemBar2COLOUR(); ItemBar3COLOUR(); ItemBar4COLOUR(); ItemBar5COLOUR(); // here is a call to the function in which the call is contained. // By Setting the number, the number of milliseconds of the // duration of the Loop Timer, is established. // Therefore // In order to have an even event duration, the events have got // to be timed, themselves, in a ratio of their number // to the length of time in milliseconds, which the Loop will Occupy. CommenceAnnoyance = window.setTimeout(SetThemUP,800); // SetThemUP() end of MAIN TIMER Loop :: psuedo~animation } /* ********************** */ /* ********************************* */ // Here begins parts of CODE suggested by Gordon. // while this is called by the *Reset* button to reset the divs // to their *Loop=OFF* appearance, AND clear the text // from the DIV which displays one after another, the // GoTime of the PerLoop Iterations of the MAIN LOOP. function DestroyTheMonster() { Stop(); Reset(); FTWxAnotherReset(); // that is a kludgy desperation~ploy // to try to get rid of the unwanted .class // which is still applied, and to reset all // 5ive to the NON~running state... appearance. } // This is the *** OFF SWITCH *** for the MainTimerLOOP. function Stop() { window.clearTimeout(CommenceAnnoyance); } function Reset() { var Aaaiyeeah = document.getElementById('Aaaiyeeah'); while (Aaaiyeeah.firstChild) { Aaaiyeeah.removeChild(Aaaiyeeah.firstChild); } Div1ne.className ="AsBeforeBubba"; Div2wo.className ="AsBeforeBubba"; Div3hree.className ="AsBeforeBubba"; Div4our.className ="AsBeforeBubba"; Div5iver.className ="AsBeforeBubba"; } function FTWxAnotherReset() { var Aaaiyeeah = document.getElementById('Aaaiyeeah'); while (Aaaiyeeah.firstChild) { Aaaiyeeah.removeChild(Aaaiyeeah.firstChild); } Div1ne.className ="AsBeforeBubba"; Div2wo.className ="AsBeforeBubba"; Div3hree.className ="AsBeforeBubba"; Div4our.className ="AsBeforeBubba"; Div5iver.className ="AsBeforeBubba"; } #TitleGhost{ position:absolute; top:11px; left:295px; width:496px; height:29px; background-color:#8ADFFB; font-size:19px; font-weight:bold; font-style:italic; font-variant:small-caps; color:black; border:3px solid navy; padding-top:3px; } #ControlPlace{ position:absolute; top:11px; left:19px; width:142px; height:42px; background-color:#33EFEA; border:2px solid buttonface; } #ControlPlace button{ position:absolute; top:6px; left:13px; width:112px; height:29px; background-color:buttonface; font-family:bookman old style; font-size:12px; font-weight:bold; color:navy; } #ControlPlaceSTOPPA{ position:absolute; top:72px; left:19px; width:142px; height:42px; background-color:#33EFEA; border:2px solid buttonface; } #ControlPlaceSTOPPA button{ position:absolute; top:6px; left:13px; width:112px; height:29px; background-color:buttonface; font-family:bookman old style; font-size:12px; font-weight:bold; color:navy; } #Aaaiyeeah{ /*Displays Unix TimeSTAMP of Each Loop Begin~Time*/ position:absolute; top:11px; left:819px; width:131px; height:430px; background-color:#C79C96; /* Impious Colours ="the lost art" */ color:#A48F04; /* Impious Colours ="GoldyGoldfish" */ border:2px solid black; overflow-y:scroll; } /* Main Display Box */ #BeginLoopingLikeAMadBastard{ position:absolute; top:137px; left:295px; width:492px; height:298px; background-color:#333333; border:5px solid purple; } /* 5 Divs In the main display */ #Div1ne.defaultBORED{ position:absolute; top:12px; left:11px; width:112px; height:29px; } #Div1ne.springME{ position:absolute; top:12px; left:11px; width:112px; height:29px; } #Div2wo.defaultBORED{ position:absolute; top:61px; left:11px; width:112px; height:29px; } #Div2wo.springME{ position:absolute; top:61px; left:11px; width:112px; height:29px; } #Div3hree.defaultBORED{ position:absolute; top:110px; left:11px; width:112px; height:29px; } #Div3hree.springME{ position:absolute; top:110px; left:11px; width:112px; height:29px; } #Div4our.defaultBORED{ position:absolute; top:159px; left:11px; width:112px; height:29px; } #Div4our.springME{ position:absolute; top:159px; left:11px; width:112px; height:29px; } #Div5iver.defaultBORED{ position:absolute; top:208px; left:11px; width:112px; height:29px; } #Div5iver.springME{ position:absolute; top:208px; left:11px; width:112px; height:29px; } #Div1ne.AsBeforeBubba{ position:absolute; top:12px; left:11px; width:112px; height:29px; background-color:#EEff78; border:2px solid #FC3B03; } #Div2wo.AsBeforeBubba{ position:absolute; top:61px; left:11px; width:112px; height:29px; background-color:#EEff78; border:2px solid #FC3B03; } #Div3hree.AsBeforeBubba{ position:absolute; top:110px; left:11px; width:112px; height:29px; background-color:#EEff78; border:2px solid #FC3B03; } #Div4our.AsBeforeBubba{ position:absolute; top:159px; left:11px; width:112px; height:29px; background-color:#EEff78; border:2px solid #FC3B03; } #Div5iver.AsBeforeBubba{ position:absolute; top:208px; left:11px; width:112px; height:29px; background-color:#EEff78; border:2px solid #FC3B03; } /* defining some .classNames, for some css statements controlled by the JavaScript */ #Div1ne.springME{ background-color:forestgreen; border:4px solid red; } #Div2wo.springME{ background-color:forestgreen; border:4px solid red; } #Div3hree.springME{ background-color:forestgreen; border:4px solid red; } #Div4our.springME{ background-color:forestgreen; border:4px solid red; } #Div5iver.springME{ background-color:forestgreen; border:4px solid red; } #Div1ne.defaultBORED{ background-color:#333333; border:2px solid white; } #Div2wo.defaultBORED{ background-color:#333333; border:2px solid white; } #Div3hree.defaultBORED{ background-color:#333333; border:2px solid white; } #Div4our.defaultBORED{ background-color:#333333; border:2px solid white; } #Div5iver.defaultBORED{ background-color:#333333; border:2px solid white; } Commence !!! Stop Madness!   This Is JavaScript.  Looping OR at Least Pretending To. |
对于没有自己的堆栈但好奇的人,请看
哦…你是认真的吗?????
看看这是否有效…好吧,伙计们,你们的功能有一个漏洞。我只是花了45分钟或一个小时手动格式化450行,然后考虑添加一个链接,得到臭名昭著和恼人的OOOP!反jsfiddle.net的废话?普莱莱斯!S.O.似乎远远高于该服务,服务于不同的目的…在内部减轻或合并类似的功能;只是不理解对它的偏见。我确实理解在S.O.有大量代码的愿望。但是当后端系统抱怨一个链接时,mebbe会很好的调整,neh?咆哮
仍在解决问题…我不接受必须点击两次,一次停止动画/循环,一次重置5个div的类名。
除了任何奇怪的种族条件(由于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function DestroyTheMonster() { Stop(); } function Takes2woToTaengo() { Stop(); setTimeout("Reset()", 800); // this is the delay } function Stop() { window.clearTimeout(CommenceAnnoyance); } function Reset() { var div = document.getElementById('Aaaiyeeah'); while (div.firstChild) { div.removeChild(div.firstChild); } Div1ne.className ="AsBeforeBubba"; Div2wo.className ="AsBeforeBubba"; Div3hree.className ="AsBeforeBubba"; Div4our.className ="AsBeforeBubba"; Div5iver.className ="AsBeforeBubba"; } |