2

hello everybody i want to refresh controller function after 5 sec automatically i am using header('Refresh: 10.2'); inside controller function like

public function delete()
    {

        header('Refresh: 10.2');
        $current_time=date('H:i:s',strtotime("+3 hours"));
        $email=$this->session->userdata['email'];
        $end_time=$this->model_user->get_res_endtime($email);
        if($current_time>$end_time || $current_time <'00:59:59')
        {
            $this->model_user->move_reserve($end_time,$email);

            $this->model_user->up_qty_del($email);

            $this->model_user->delete_reserve($end_time,$email);
        }

    }

but this refresh the whole page also have any method that will refresh only function? thanks in advance

3
  • Create seperate function for this and call it via AJAX every 5 seconds. Commented Aug 14, 2015 at 15:07
  • @llan Hasanov can you tell the code? Commented Aug 14, 2015 at 15:09
  • I have posted the Answer for you, let me know how it goes.. Commented Aug 14, 2015 at 15:19

1 Answer 1

1

For your request.. you can try something like this. Controller:

public function delete()
{

    $current_time=date('H:i:s',strtotime("+3 hours"));
    $email=$this->session->userdata['email'];
    $end_time=$this->model_user->get_res_endtime($email);
    if($current_time>$end_time || $current_time <'00:59:59')
    {
        $arr["message"] = "1";
        $this->model_user->move_reserve($end_time,$email);
        $this->model_user->up_qty_del($email);
        $this->model_user->delete_reserve($end_time,$email);
        print(json_encode($arr));
        exit;
    }

}

The jQuery:

$(document).ready(function () {
    (function delete() {
        //run the request
        $.ajax({
            type: "POST",
            url: "Controller/Deletefunction",
            async: true, //Hey browser! run the code
            //if we prased data run:
            success: function (data) {
                /* we prase data (data can be anything)
                 * data is json_encoded data format coming from
                 */
                var obj = JSON.parse(data);
                // if we prased data:
                if (obj) {
                    console.log(obj);
                    // chrome console log should show here "success"
                    if (obj.count > 0) {
                        // Schedule the next request when the current one's complete
                setTimeout(delete, 5000);
            }
        });
    })();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.