Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two functions in Javascript, one waits for first execution of a button and waits for second button to fire when it reads first value(field8=0) and continue to second value(field8=1). But the problem i want a way to stop iteration, the iteration continues without stopping. How do i read first value as being 0 and vice versa? Be able to show status message to a user if that value is 0 or 1? So that when it finds it continue until it stop iteration process in Ajax/Javascript. Please help, thanks.

What I have tried:

JavaScript
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
  
  function Subscribe() {
    $.ajax({
      url:'https://api.thingspeak.com/update?api_key=***&field8=0',
      type:'GET',
      data:{
      type:'text'
      },
      success:function(response){
        alert(response);
          $('#singlebutton').append(data);
         }
    
    });
    setTimeout("Subscribe()", 100);
  
    }
  // second button to unsubscribe.
  function UnSubscribe() {
    $.ajax({
      url:'https://api.thingspeak.com/update?api_key=***&field8=1',
      type:'GET',
      data:{
      type:'text'
      },
      success:function(response){
        alert(response);
          $('#singlebtn').append(data);
         }
    
      });
    //setTimeout("UnSubscribe()", 100);
  }
    

</script> 
Posted
Updated 6-Nov-19 23:29pm
v3

1 solution

Window clearTimeout() Method[^]

You need to get a handle from your timer

var h = setTimeout("Subscribe()", 100);


then when you want to stop the iteration call

clearTimeout(h);
 
Share this answer
 
Comments
gcogco10 7-Nov-19 5:41am    
This seem to work especially clearTimeout, now problem i am getting now. Each time button is clicked more than 3 times, it does not keep previous value. Like an instance of Unsubscribe function() field value is set on url as field8=1. it default to 0 on the browser. Is there a better way to improve from this logic as i am trying to work on that as we speak?. What i want exactly is, if the Suscribe function() keep value as 0, then value must be kept to that original value, not to change whether the browser was refreshed or clicked 3 times. Same applies to Subscribe function(). Please i am battling to get this logic right.
F-ES Sitecore 7-Nov-19 6:14am    
Not sure I really understand what you're trying to do, but it sounds like you need to update the url in the browser so that refreshing the page keeps your desired value. If that's the case you need to use the pushState function of the history api

https://developer.mozilla.org/en-US/docs/Web/API/History_API
gcogco10 7-Nov-19 7:15am    
F-ES Sitecore, what i am trying to do simple. I want my value on URL from ajax call field8=1 not to update twice must update once. While field8=0 must remain its original value not to update that from field8=1, thats is what is happening currently if the button is clicked from the browser. In another words there is mismatched of value between the buttons, if the one clicked first and vice versa.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900