2017-07-26 91 views
0

我嘗試在我的頁面上運行通知消息,但我只是想知道是否有任何簡單的方法來編碼?如何每隔5秒每次運行一個函數,並使用差值?

<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> 

我該如何使用很簡單的代碼來運行這些功能?對不起,在編程和自學習方面我很新。

+0

這段代碼實際上做了什麼? –

+0

izi吐司通知插件。 http://izitoast.marcelodolce.com/ –

回答

3

我認爲,如果你想重複在PHP中您可以使用循環功能,你需要這個

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); 
    } 
+0

謝謝你的回答,但我的標題和消息是差異名稱而不是數字? –

+0

我編輯並從麥Kobi選項回答@ChiewSoonHow –

+0

非常感謝你 –

0

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

,如果你想重複一個函數在JavaScript中,你可以使用下面的代碼:如果你想重複的功能爲指定的時間,你可以使用下面的代碼

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

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

嘗試window.setInterval();

例如:

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

上述代碼每12500ms運行一次。 嘗試找出一種方法來爲每個時間間隔傳遞一個動態對象。

2

也許是這樣的:

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]); 
} 

對於另一敬酒,你可以添加一個條目來選擇和超時超時陣列。

+0

感謝您的幫助。 –

+0

您好我已經嘗試了代碼,但選項數據沒有顯示在彈出窗口中。代碼有沒有問題?請幫忙 –