I'm creating a function that checks id values for 0 or 1. This function is to perform following task (Triggering SMS function):
1) If on page load, value of any id is 1(true), this function will not allow sms trigger for this id, but if value goes to 0(false) and again returns to 1(true) it will trigger a sms function.
2) If on page load, value of id is 0(false), this function will allow SMS triggering if its value changes to 1 (true) any time.
Note: When I check this function with value1 or value2or value3 separately it is working fine. But when I execute this funciton for multiple ids like onloadcheck(value1); onloadcheck(value2); it creates problem. No error but not working in proper manner.
Please help me. Thank you.
$(document).ready(function() {
setInterval(function() {
rand_val();
}, 1000);
});
//check initial values of all ids
var onloadcheck = (function() {
var executed = false;
var enableit = false;
return function(x) {
if (!executed) {
executed = true;
if(x==1){enableit = false;}
if(x==0){enableit = true;}
}
if(x==1 && enableit ){
console.log("Sending SMS");
enableit = false;
}
if(x==0 ){
enableit = true;
}
};
})();
//generates random values
function rand_val(){
document.getElementById("demo1").innerHTML =
Math.floor(Math.random() * 2);
var value1= $("#demo1").text();
document.getElementById("demo2").innerHTML =
Math.floor(Math.random() * 2);
var value2= $("#demo2").text();
document.getElementById("demo3").innerHTML =
Math.floor(Math.random() * 2);
var value3= $("#demo3").text();
onloadcheck(value1);
onloadcheck(value2);
}
Here is HTML code :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test SMS</title>
<style>
</style>
</head>
<body>
<p>Test SMS Triggers</p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
</html>
if(x==1 && enablesms ){did you meanenableitto go here? As i don't see aenablesmselsewhere in your code