Skip to content

Commit

Permalink
preventing infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiliavir committed Jan 30, 2023
1 parent 232f7b0 commit c33460f
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions assets/ts/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,37 @@ class Register {
"Tuba": ["tuba_1.jpg", "tuba_2.jpg"]
};

public static tryReplaceImage($images: JQuery): boolean {
const $image: JQuery = $($images[Math.floor(Math.random() * $images.length)]);
if (Register.isElementInViewport($image[0])) {
const remainingRegImageUrls = Register.register[$image.attr("title")]
.filter(url => !Register.endsWith("/" + $image.attr("src"), url));
if (remainingRegImageUrls.length > 0) {
$image.fadeOut("fast", (): void => {
const nexIndex = Math.floor(Math.random() * remainingRegImageUrls.length);
$image.attr("src", "/img/register/" + remainingRegImageUrls[nexIndex]);
$image.fadeIn("slow");
});
return true;
public static tryReplaceImage($images: JQuery): void {
let htmlElements = this.shuffle($images);
for (let image of htmlElements) {
if (Register.isElementInViewport(image)) {
const $image = $(image);
const remainingRegImageUrls = Register.register[$image.attr("title")]
.filter(url => !Register.endsWith("/" + $image.attr("src"), url));
if (remainingRegImageUrls.length > 0) {
$image.fadeOut("fast", (): void => {
const nexIndex = Math.floor(Math.random() * remainingRegImageUrls.length);
$image.attr("src", "/img/register/" + remainingRegImageUrls[nexIndex]);
$image.fadeIn("slow");
});
break;
}
}
}
return false;
}

private static shuffle(array: JQuery): JQuery {
for (let m = array.length, t, i; m > 0; m--) {
i = Math.floor(Math.random() * m);

t = array[m - 1];
array[m - 1] = array[i];
array[i] = t;
}

return array;
};

private static endsWith(str: string, suffix: string): boolean {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
Expand All @@ -49,11 +63,5 @@ class Register {

$(() => {
const $images = $(".mvw-register-table img");
setInterval(
(): void => {
while (!Register.tryReplaceImage($images)) {
// find an image to replace...
}
},
8000);
setInterval(() => Register.tryReplaceImage($images), 8000);
});

0 comments on commit c33460f

Please sign in to comment.