Been trying to get to the bottom of this for a couple of days now. For some reasons the mozMobileMessage function doesn't seem to work properly with setIntervals (using the script on KaiOS 2.5 app). The code below (where the mobile number is captured as part of the app) only ever sends a single SMS message to the phone. Any idea how to fix this? I'd like the message to be sent at set intervals for a set duration:

let timerId = setInterval(navigator.mozMobileMessage.send(mobileNumberInput.value, "Hello"), 10000);
setTimeout(() => {clearInterval(timerId);}, 40000);

I've tried to call mozMobileMessage several times separately within the code as per below to check if this could be to do with some function limitations. But calling the function manually within the code will work fine and sends an SMS as many times as the functions is called:

navigator.mozMobileMessage.send(mobileNumberInput.value, "Hello");
navigator.mozMobileMessage.send(mobileNumberInput.value, "Hello");
navigator.mozMobileMessage.send(mobileNumberInput.value, "Hello");
1

There are 1 answers

2
Yuvaraj M On

You are passing invalidate parameters to setInterval function.

By shorthand, you can to use this syntax for both setInterval and setTimeout

setInterval(function_name,delay,...parameters_for_function_to_be_called);

Explanation

  1. First parameter is a function name, you don't have to call it initially timers will do for you.

  2. Second parameter is delay obviously in seconds.

  3. Finally remaining passed parameters are be the arguments for your function that passed in first parameter.

Let says for example, I have the following function

function sendMessage(number,message){
   console.log(number, message);
}
 
const interval = setInterval(sendMessage,1000,1234567890,'hello');
   
setTimeout(clearInterval,2000,interval);
.as-console-wrapper { max-height: 100% !important; }

In your case it will be like

const timerId = setInterval(navigator.mozMobileMessage.send,10000,mobileNumberInput.value, "Hello");
setTimeout(() => {clearInterval(timerId);},