简体   繁体   中英

Sequence of functions in jquery

i've started recently to use jquery for a Tampermonkey script, and it's not doing the logical sequence in the functions. This is the code:

$(document).ready(function(){
    if ($(window).attr('location') == 'http:1') {
        $(window).attr('location', 'http:2');
    }
});

$(document).ready(function(){
    for(var i=2; i<7; i++){
        $elem = $('div#ing-' + i).find('span').css('color');
            if ($elem == 'rgb(255, 0, 0)'){ 
                $(window).attr('location', 'http:3...&r=' + i);
        }           
    }
    if($(window).attr('location')=='http:2'){
        $(window).attr('location', 'http:4');
    }
    $('#solicitar').trigger('click');
});  

I have two problems,

First: The last function

if($(window).attr('location')=='http:2'){
        $(window).attr('location', 'http:4');
    }
    $('#solicitar').trigger('click');

is the one that's always running, it doesn't run the FOR function, it just jump to that part and do that.

Second: If i just run the loop FOR (ignoring the other function), the first instruction to be ejecuted it's when i=6 and i=2 is the last one, that doesn't makes sense at all for me.7

EDIT: if my html(2) = ' http://www.aaa.com ' and html(3 and 4) = http://www.aaa.com/index.php?p=bbb The location attribute treats them like the same?

SOLVED. I just need to add a delay, because the code was runnning faster than the http could refresh.

setTimeout(function(){
     if($(window).attr('location')=='http:1'){
        $(window).attr('location', 'http:2');
     }}, 5000);
    $('#solicitar').trigger('click');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM