Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript window.setInterval(function() Need help

  • This topic is empty.
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #191705
    tarnos12
    Participant

    Hey, for some reason my function is not giving me the result I expected.

    http://jsfiddle.net/u6dc0m4z/18/

    Here is the link to the game:
    http://tarnosdaking.esy.es/public_html/

    even without upgrading any stats, just try to lose some hp click on a first monster, and you will notice that your hp increase 2 times in a split of a second and give you total 4hp instead of 2 per second, I didnt notice this bug earlier. Help me out, I search my whole code for hpregen and there is only this one part you can see on jsfiddle above.

    #191758
    tarnos12
    Participant

    How can it be called again? When it’s supposed to be called once a second? Though I will try to clear this interval somehow and see the effect. Thanks for the answer. The problem is that, jssfiddle code I sent is working, only the code inside my main file isnt working, could it be because I am using more than one set interval?

    #191808
    tarnos12
    Participant

    Thanks for the answer, I will try to do that, hopefully I don’t need to use a button for that ^^

    #191845
    tarnos12
    Participant

    Thing is, there is nothing that “start” this interval…it just works on its own.

    window.setInterval(function()
    {
    
    if (health + hpregen >= maxhealth)
    {
        health = maxhealth;
        document.getElementById('health').innerHTML = health;
    }
    else if (health + hpregen < maxhealth)
    {
        health = health + hpregen;
        document.getElementById('health').innerHTML = health;
    
    }
    }, 1000);
    

    thats all :> there are just global variables.

    #191874
    tarnos12
    Participant

    Hey thanks for the reply for a short code, but still I can’t make it work for me. I will try to find an other way for interval function maybe. If there is any other. You can check my first post for the game link and check main.js file its long really long but you might find a bug :> btw I tried to delete all “intervals” except for hpregen and it still didnt work. Also you could give me some tips on coding, right now my code is like 6000 lines long, which I could probably make using much less code. This bug is the same for all intervals I made. Not sure what do you mean by “stalling later” but if you set maxhp it is supposed to stop once it reach that maxhp Thanks for all help :)

    “edit” Also that short code you put “else health += hpregen;” wont this cause my health go over maxhealth? if my max health is 30 and I have 29, first “if” wont work because its not equal to 30 or higher than it, so the second will give my health = 29+ hp regen but of course first function will reset it after 1 second, but that 1 second I will have more health that I should have.

    #191880
    tarnos12
    Participant

    Hey, thanks for answers, first of all I dont have jquery installed. Since I am really new to all this I am almost 100% sure that I didnt even install bootstrap properly, could you check my html file and see if there are some problems? It seems there is a 1.9.1 version of jquery which bootstrap can use, but I don’t know how to install it. When I put your code for

    $(document).ready(function()

    My whole script is not working anymore, I have no idea why. I put this in my html file:

    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;

    I will keep trying to figure this out.

    @edit:

    The reason why I think, my bootstrap isnt working is:

    &lt;div class="progress-bar progress-bar-success progress-bar-striped"

    I cant make it work :| so I am confused if it is working or not.

    #191889
    tarnos12
    Participant

    haha you are the best :) I knew there is a simple mistake…I update my website code, while you were looking at my html code, I changed some links, not sure if I did good or not but if you can check it again :) Thanks again really. Also yes that bracket is a typo, I did copy whole code and all, but i dont need it anymore I guess.

      <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    

    Those are the only scripts I have right now, I hope that is enough, still I cant make progress bar color change. I will try to figure this out :) I will also change all my interval codes the way you showed me, to make it smaller. Thanks again for all your help.

    #191895
    tarnos12
    Participant

    Hey, thanks again I will check those “red” part of the code, I feel like this is the reason some of my stuff isn’t working. I tried again to wrap my code with your function, but its not working, I don’t know what am I doing wrong. When I try to do something with this code I get error “…is not defined” I guess there must be some small detail like ; missing I don’t know. I will check jquery, since it is installed on my website I guess, I should make it work.

    #191898
    tarnos12
    Participant

    I see, thanks, right now I want to change my code so instead of 32x onclick functions x32 battle/exp/gold/item drop code I can put it all into 1 function, because each monster or item I add I need to change all of that, which is annoying -_- so I decided to check out objects in javascript and see how it works. I hope it will work out, since I dont want to edit x100 part of the code if I decide to change damage/exp etc formula. Also I deleted all of that “red” part of the code and it works good :) thanks for that

    #191902
    tarnos12
    Participant

    Hey thanks for your answer, but I dont understand what do you mean by “don’t let W3 rain on the parade though.” Also that validation page you sent me, should I consider getting rid of all problems?

    @edit:

    I just noticed it but my whole page can be scrolled further to the right side :/

    #191905
    tarnos12
    Participant

    Hey, thanks for reply, first of all I did try css text align, but can’t make it work for my elements inside tables, so I tried using divs and center them, also didn’t work, but I will try again soon, next thing is I deleted all containers and left only main, which didnt work, my page became a mess.

    I tried to remove this part but my page again became a mess :>

    #191908
    tarnos12
    Participant

    I cleaned up html file, it should be better now :) still can’t find a reason why I can scroll to the right, I am still looking into it, but I want to focus on actual game so I will do it next day maybe.

    Could not edit last post so I had to make a new one.

    #191931
    tarnos12
    Participant

    Hey I changed my html again, so there is no longer scrolling to the right :) all that is left is to learn javascript objects and some other stuff, so I can put half of my code in a single function instead of 30 functions for each button -_- I will do that later tho, I did’t sleep since our first post yestarday :|

    #191972
    tarnos12
    Participant

    Hey, thanks I will do my best. If I ever have a question I will ask here again ^^

Viewing 14 posts - 1 through 14 (of 14 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.