jump to specific line in jquery

  javascript, jquery

I have a txt file with many lines of input, and a code that reads the file line by line that works perfectly. Now I would like to improve the code by adding a button that would jump to a specific sentence in the text, but I’m having a lot of trouble with it.

my code is

fetch('pbp/<?php echo $pid;?>.txt')
.then(response => response.text())
.then(text => {
    var lines = text.split('n');
    (async  () => {
        for(var line = 0; line < lines.length; line++){
            await delay();

            if(lines[line].includes('ball:')){
                console.log('aqui');
                changeShirt(lines[line]);
            }

            if(!lines[line].includes('---') && lines[line].length > 0){
                document.getElementById('pbp').innerHTML = lines[line];
            }
            if(lines[line].includes('Quarter  Time Remaining')){
                document.getElementById('scoreboard').innerHTML = lines[line];
            }
            // Check positions and set animate ball
            if(lines[line].includes('(Deep Right wing)')){
                $("posicao").animate({left: "414", top: "75"}, 1000);
                $('#DeepRightwing').css('display','none')
            }else if(lines[line].includes('(Left wing)')){
                $("#posicao").animate({left: "308", top: "349"}, 1000);
            }else if(lines[line].includes('(Deep Left wing)')){
                $("#posicao").animate({left: "431", top: "375"}, 1000);
            }else if(lines[line].includes('(Right wing)')){
                $("#posicao").animate({left: "314", top: "116"}, 1000);
            }else if(lines[line].includes('(Left corner)')){
                $("#posicao").animate({left: "242", top: "442"}, 1000);
            }else if(lines[line].includes('(Right corner)')){
                $("#posicao").animate({left: "257", top: "25"}, 1000);
            }else if(lines[line].includes('(Left baseline)')){
                $("#posicao").animate({left: "208", top: "334"}, 1000);
            }else if(lines[line].includes('(Right baseline)')){
                $("#posicao").animate({left: "212", top: "132"}, 1000);
            }else if(lines[line].includes('(Post)')){
                $("#posicao").animate({left: "304", top: "232"}, 1000);
            }else if(lines[line].includes('(Deep Post)')){
                $("#posicao").animate({left: "253", top: "232"}, 1000);
            }else if(lines[line].includes('(FT Line)')){
                $("#posicao").animate({left: "414", top: "234"}, 1000);
            }else if(lines[line].includes('free throw')){
                $("#posicao").animate({left: "414", top: "234"}, 1000);
            }else if(lines[line].includes('(Top of the key)')){
                $("#posicao").animate({left: "492", top: "234"}, 1000);
            }else if(lines[line].includes('It's Good') || lines[line].includes('for the basket')){
                $("#posicao").animate({top: "233", left: "214"}, 1000);
            }else if(lines[line].includes('The first free throw') || lines[line].includes('The second free throw')){
                $("#posicao").animate({top: "157", left: "286"}, 3000);
            }else if(lines[line].includes('No good')){
                $("#posicao").animate({top: "233", left: "214"}, 1000);
                console.log('aqui');
                
            }
        }
    })();
})

// Set delay time for mensage and animate
function delay() {
    return new Promise((resolve, reject) => {
        // Time in milliseconds. 1000 = 1 seconds, 2000 = 2 seconds
        setTimeout(resolve, 1000);
    });
}

function changeShirt(shirtTeam){
    // Get team name
    var imageName = shirtTeam.split(" ");
    // Get all shirt image
    var elms = document.getElementsByClassName('shirt')

    for (var i = 0; i < elms.length; i++) {
        // Change all image using team name
        elms[i].setAttribute("src", imageName[0]+".png");
    }
}

function timer(ms) { return new Promise(res => setTimeout(res, ms)); }
</script>

The code reads basketball play by play and animates every move, but as each file contains more than 1000 lines, i would like to create a button that would jump to the final 2 minutes of gameplay.

Source: Ask Javascript Questions

LEAVE A COMMENT