setTimeout()函数未正确执行

时间:2017-04-21 11:46:32

标签: node.js

我必须在约会前15分钟发送短信。我编写了代码,但在执行时调用它。有没有其他方法或如何解决这个问题?

“RP.remindePatient”是必须在约会时间前15分钟调用的函数。

sendApptConf(s) {

    var number = Phone;
    var msg = urlencode("Hello " );

    var smsData = 'username=' + username + '&hash=' + hash + '&sender=' + sender + '&numbers=' + number + '&message=' + msg;
    var options = {

        host: 'api.textlocal.in',
        path: '/send?' + smsData
    };

    var callback;
    console.log(options);
    callback = function (response) {
        var str = '';

        // Another chunk of data has been recieved, so append it to `str`
        response.on('data', function (chunk) {
            console.log("new Data received")
            str += chunk;
            console.log(str);
        });

        // The whole response has been received, so we just print it out here.
        response.on('end', function () {
            console.log(str);
        });
    }

    http.request(options, callback).end();
    // Send SMS using Textlocal DONE

    const convertTime = consultDate + ' ' + consultTime;

    var d = new Date(convertTime);
    var ms = d.getTime();
    var milliLess15 = ms - (15 * 60 * 1000);

    console.log(milliLess15);

    setTimeout(function () {

        console.log("I should be called after some delay")
        RP.remindePatient(userPhone, patientName, drName, consultMode, consultDate, consultTime, transId, email, paymentDetails);

    }, milliLess15);

2 个答案:

答案 0 :(得分:0)

暂停后不要调用变量。刚加入:

setTimeout(function () {
console.log("I should be called after some delay")
       RP.remindePatient(userPhone, patientName, drName, consultMode, consultDate, consultTime, transId, email, paymentDetails);

     }, 900000);

答案 1 :(得分:0)

我认为你的逻辑中存在一个小问题。如您所知,setTimeout(function () {}, milliseconds)将在您指定的function()之后分叉milliseconds

现在让我们关注下面的代码片段:

 const convertTime = consultDate + ' ' + consultTime;
 var d = new Date(convertTime);
 var ms = d.getTime();
 var milliLess15 = ms - (15 * 60 * 1000);

让我们说你的预约是今天中午12点(以毫秒为单位,比如1492776423),当前执行时间是上午10:00(以毫秒1485576423为单位)。根据您的逻辑,您在function()之后调用var milliLess15 = ms - (15 * 60 * 1000) 1492776423 - (15 * 60 * 1000) = 1491876423,即function()。请注意,您不想在1491876423毫秒之后致电12:00 - 10:00 - 00:15 。相反,你想在毫秒级相当于const convertTime = consultDate + ' ' + consultTime; var d = new Date(convertTime); var ms = d.getTime(); var currentTime = new Date(); var currentTimeMilli = currentTime.getTime(); var milliLess15 = ms - (15 * 60 * 1000) - currentTimeMilli; 之后调用它。所以你的逻辑看起来应该是这样的:

class Users_Model extends CI_Model
{

    public function getDownline(User_Object $obj, $level = 0) 
    {
        $obj->level = $level;

        $where = array('parent_id' => $obj->id);
        $this->db->where($where);
        $query = $this->db->get('users')->result("User_Object");

        foreach ($query as $objUser) 
        {
            $obj->add($objUser);
            $this->getDownline($objUser, ($level+1));
        }
        return $obj;
    }

    public function downline_income($userId = null, $offset = 0) 
    { 
        $userId = user::id(); 
        $objUser = new User_Object;
        $objUser->id = $userId;
        $downline = $this->user->getDownline($objUser);
    }
}


class User_Object
{
    private $children = [];
    public $level = 0;

    public function add(User_Object $obj)
    {
        $this->children[] = $obj;
    }

}

其余代码将保持不变。希望这个答案可以帮到你!