How can I set a cookie value when a user leave the page?

How can I set a cookie value when a user leave the page?

Hi
I have a seconds counter in my page and I want to keep the counter when a user jump between pages. So I though I can save the total seconds in cookies variable when the user leave the page and then retrieve it when ever they come back to the page.


this is what I have currently but I want to trigger this even when the user navigate out the active page

var currentCount = $.jCookie('callTimer');
$.jCookie('callTimer', Number(currentCount) + 1000  );
    }, 1000);


also how can I check if the cookies is not yet set?

Thanks