The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Update only myTotal if newvalue > currentValue
sanoj96
post Dec 5 2020, 02:00 PM
Post #1


Advanced Member
****

Group: Members
Posts: 118
Joined: 18-September 12
Member No.: 17,803



Hello,

I am trying to update mytotal when newValue > currentValue.


Something like this.

CODE

If (newValue > currentValue) {
    //Update mytotal on page (Document.write). And not reload the whole page.
}, 2000); // every 2 second.


//I know i can do something like this, but that will reload the page and not just the mytotal on the page.
        $(document).ready(function() {
            // auto refresh page after 2 seconds
            setInterval('refreshPage()',  2000);
        });
    
        function refreshPage() {
            location.reload();
        }


This is the working code i have for now

CODE

        $(document).ready(function() {
            // auto refresh page after 1 second
            setInterval('refreshPage()',  2000);
        });
    
        function refreshPage() {
            location.reload();
        } // Change this to update only mytotal and not reload the page.
    var myArray;

        $.getJSON( "JsonFile.json", function( json ) {
          myArray = json;
          
          //console.log(myArray); // for Debuging
            var myTotal = 0;  

            for(var i = 0, len = myArray.length; i < len; i++) {
                myTotal += myArray[i].Amount;  
            }
            
            document.write("<span style='font-size: 50px;'>Total:  "+ myTotal + "kr");

         });


This is how the json file.
CODE

[
   {
      "DonationId":"DonationId1",
      "Name":"Name Of Dono 1 ",
      "Amount":250.0000,
      "Message":"This is a custom message from Name",
      "MessageAnswer":"",
      "CollectorImageUrl":null,
      "CurrencySymbol":"$",
      "CollectionUrl":"",
      "TransactionDate":"03.12.2020"
   },
   {
      "DonationId":"DonationId2",
      "Name":"Name Of Dono 2 ",
      "Amount":100.0000,
      "Message":"This is a custom message from Name",
      "MessageAnswer":"",
      "CollectorImageUrl":null,
      "CurrencySymbol":"$",
      "CollectionUrl":"",
      "TransactionDate":"03.12.2020"
   },
   {
      "DonationId":"DonationId3",
      "Name":"Name Of Dono 3 ",
      "Amount":500.0000,
      "Message":"This is a custom message from Name",
      "MessageAnswer":"",
      "CollectorImageUrl":null,
      "CurrencySymbol":"$",
      "CollectionUrl":"",
      "TransactionDate":"03.12.2020"
   },
]



Reason for why i want to only update mytotal, is because when the page reloads, it goes blank for a sec before showing the value.

So i am wondering if anyone of you guys know how i can reload just "myTotal" and not the page.

Sorry for bad english, but i hope you understand.

Thanks,
sanoj96
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Dec 5 2020, 02:37 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Don't use document.write anywhere. All you need is an input to hold the value (myTotal). Then use JS to update that value or innerHTML, depending on which element you use. No page reload or setInterval needed at all. You can even tie the calculation to some event. You should have a very good reason to use document.write and based on what you have said this is NOT one of those times.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
sanoj96
post Dec 5 2020, 08:34 PM
Post #3


Advanced Member
****

Group: Members
Posts: 118
Joined: 18-September 12
Member No.: 17,803



QUOTE(CharlesEF @ Dec 5 2020, 02:37 PM) *

Don't use document.write anywhere. All you need is an input to hold the value (myTotal). Then use JS to update that value or innerHTML, depending on which element you use. No page reload or setInterval needed at all. You can even tie the calculation to some event. You should have a very good reason to use document.write and based on what you have said this is NOT one of those times.


Thanks for the Pointers! I managed to get it working!

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th April 2024 - 06:26 AM