Chiew Soon How
Chiew Soon How

Reputation: 5

How to run a function every 5 sec with difference value everytime?

i try to run a notification message on my page but i just wondering is there any siimple ways to code this?

<script>
    window.setTimeout(function(){
            iziToast.show({
                    hyperlink: '?id=002',
                    title: 'Title 2',
                    message: 'message 2',
                    image: 'img/2.jpg',
                    timeout: 7500,
                });
    }, 5000);

    window.setTimeout(function(){
            iziToast.show({
                    hyperlink: '?id=003',
                    title: 'Title 3',
                    message: 'message 3',
                    image: 'img/3.jpg',
                    timeout: 7500,
                });
    }, 17500);

    window.setTimeout(function(){
            iziToast.show({
                    hyperlink: '?id=004',
                    title: 'Title 4',
                    message: 'message 4',
                    image: 'img/4.jpg',
                    timeout: 7500,
                });
    }, 30000);

    window.setTimeout(function(){
            iziToast.show({
                    hyperlink: '?id=005',
                    title: 'Title 5',
                    message: 'message 5',
                    image: 'img/5.jpg',
                    timeout: 7500,
                });
    }, 42500);


    </script>

How can i use much simple code to run those function? Sorry im very new in programming and self learner.

Upvotes: 0

Views: 634

Answers (4)

I think you need this

    var i = 0;
    var interval;

//taken from Ma Kobi answer

var options = [
{
    hyperlink: '?id=002',
    title: 'Title 2',
    message: 'message 2',
    image: 'img/2.jpg',
    timeout: 7500,
},
{
    hyperlink: '?id=003',
    title: 'Title 3',
    message: 'message 3',
    image: 'img/3.jpg',
    timeout: 7500,
},
{
    hyperlink: '?id=004',
    title: 'Title 4',
    message: 'message 4',
    image: 'img/4.jpg',
    timeout: 7500,
},
{
    hyperlink: '?id=005',
    title: 'Title 5',
    message: 'message 5',
    image: 'img/5.jpg',
    timeout: 7500,
}];

    //taken from Ma Kobi answer

    function myfunction() {
        interval= setInterval(function () {

            iziToast.show(options[i]);

            i++;

            if (i == 6) {
                i = 0;
                clearInterval(interval); 
            }

        }, 1000);
    }

Upvotes: 3

user3423927
user3423927

Reputation: 61

Try window.setInterval();

Ex:

setInterval(function(){ 
    alert("Hello"); 
},12500);`

The above code runs for every 12500ms. Try to figure out a way to pass a dynamic object for each time interval.

Upvotes: -1

Ma Kobi
Ma Kobi

Reputation: 896

Maybe so:

var options = [
    {
        hyperlink: '?id=002',
        title: 'Title 2',
        message: 'message 2',
        image: 'img/2.jpg',
        timeout: 7500,
    },
    {
        hyperlink: '?id=003',
        title: 'Title 3',
        message: 'message 3',
        image: 'img/3.jpg',
        timeout: 7500,
    },
    {
        hyperlink: '?id=004',
        title: 'Title 4',
        message: 'message 4',
        image: 'img/4.jpg',
        timeout: 7500,
    },
    {
        hyperlink: '?id=005',
        title: 'Title 5',
        message: 'message 5',
        image: 'img/5.jpg',
        timeout: 7500,
    }
];

var timeout = [
    5000, 17500, 30000, 42500
];

for (var i = 0; i < options.length; i++) {
    window.setTimeout(function(){
        iziToast.show(options[i]);
    }, timeout[i]);
}

For another toast you can add an entry to options and a timeout to timeout array.

Upvotes: 2

Milad Kahsari Alhadi
Milad Kahsari Alhadi

Reputation: 513

If you want to repeat a function in php you can use loop.

    for (init counter; test counter; increment counter) {
           code to be executed;
    } 

if you want to repeat a function in javascript you can use the following code:

for (statement 1; statement 2; statement 3) {
    code block to be executed
}

if you want to repeat a function for an specified time you can use the following code:

setInterval(function(){ 
     foFuction()
},50000;

Upvotes: -1

Related Questions