How to run function after update?

There is a function that makes the item you want to upgrade from ODA. the frequency specified in the selector, it must retain its value when you restart on the cookies.
Function:
var interval,
count
tick;

$(document).on('change', '#select', function() {
clearInterval(interval);
clearInterval(count);
 tick = 0;

$('#reload-nc').click();
 $('.timer').text(tick + ' c.');

 count = setInterval(function(){
tick++;
 $('.timer').text(tick + ' c.')
 }, 1000);

 interval = setInterval(function(){
$('#reload-nc').click();
 }, this.value * 1000);
});


A script that after upgrading to select the desired item:
jQuery(document).ready(function(){
 var selectedVal = jQuery.cookie("selected-val");
 if (selectedVal) {
jQuery("#select").val(selectedVal);
}
 jQuery("#select").on("change", function(){
 var selection = jQuery(this).val();
 jQuery.cookie("selected-val", selection, {expires: 365, path: '/'})
});
});


The main problem is that he just selected, but not activated as far as I understand, because the update did not occur, the function does not even start. How can this decision to simplify and to make it work right?
June 10th 19 at 15:31
1 answer
June 10th 19 at 15:33
Solution
The idea is something like this:
$(document).ready(function(){
 var interval,
count
tick,
 selectedVal = $.cookie("selected-val");

 function refresh() {
clearInterval(interval);
clearInterval(count);
 tick = 0;

$('#reload-nc').click();
 $('.timer').text(tick + ' c.');

 count = setInterval(function(){
tick++;
 $('.timer').text(tick + ' c.')
 }, 1000);

 interval = setInterval(function(){
$('#reload-nc').click();
 }, $('#select').val() * 1000);
}

 if (selectedVal) {
$("#select").val(selectedVal);
refresh();
}

 $("#select").on("change", function(){
 var selection = $(this).val();
 $.cookie("selected-val", selection, {expires: 365, path: '/'})
refresh();
});
});
How to simplify is you already think of yourself.
Thank you, works great
Have a nice evening) - Mireille.Hami commented on June 10th 19 at 15:36

Find more questions by tags jQuery