audiophonic
audiophonic

Reputation: 171

How can i loop my code?

My code looks like this:

$(document).ready(function() {

  var callnotification = function() {
  $.sticky('<b>TestA</b>');
  }
  setInterval(callnotification, 5000);
}); 

And i want to show different notifications, like: TestA, TestB, Test3 every 5000 seconds. I tried do something like this:

$(document).ready(function() {

var callnotification = function() {
$.sticky('<b>TestA</b>');
}
setInterval(callnotification, 5000);

var callnotification = function() {
$.sticky('<b>TestB</b>');
}
setInterval(callnotification, 5000);

var callnotification = function() {
$.sticky('<b>TestC</b>');
}
setInterval(callnotification, 5000);

}); 

But it seems to multiply. I think thats becase functions are looping each other, and it shows notifications like: Test A Test B Test B Test B Test C Test B Test A etc..

So.. how can i loop that? Lets say i want to loop from Test A to Test M and again from Test A to Test M.

Upvotes: 0

Views: 88

Answers (3)

kidwon
kidwon

Reputation: 4514

In my opinion Sticky js is a poor library.

Use this one instead: Notification Manager

Here are are few examples for your case see which one fits you:

$(function() {
  var charCode = 65;

  setInterval(function() {

    var notification = window.createNotification();
    var msg = 'Test ' + String.fromCharCode(charCode++);
    notification.addMsg({
      text: msg, type: 'alert'
    });

    notification.render(null, function(notif) {
      // example 1
      notif.destroy(5000);

      /* example 2
          notif.destroy(5000, function() {
            var newNotif = window.createNotification();
            newNotif.addMsg({
              text: '(' + msg + ' is closed)',
              type: 'info'
            });
            newNotif.render();
          });
          */


      /* example 3
          var newNotif = window.createNotification();
          newNotif.addMsg({text:'('+msg+' is closed)', type:'info'});
          newNotif.render(5000);
          */
    });

    (charCode == 78) && (charCode = 65);
  }, 1000);
});

Upvotes: 0

kidwon
kidwon

Reputation: 4514

Try this:

var i = 0;
var letters = ['A', 'B', 'C'];

var callnotification = function(msg) {
   $.sticky('<b>'+msg+'</b>');
}

var t = setInterval(function(){
   callnotification('Test'+letters[i%letters.length]);
   i++;
},5000);

Snippet: http://codepen.io/anon/pen/mPbpbm

Upvotes: 0

S. Nadezhnyy
S. Nadezhnyy

Reputation: 582

Try this:

 $(document).ready(function() {
      var callnotification = function() {
      var counter = 65;
      $.sticky('<b>Test' + String.fromCharCode(counter++) + '</b>');
      }
      setInterval(callnotification, 5000);
    }); 

For Re-loop from M to A :

$(document).ready(function() {
          var callnotification = function() {
          var counter = 65;
          $.sticky('<b>Test' + String.fromCharCode(counter++) + '</b>');
          if (counter == 78) {counter = 65};
          }
          setInterval(callnotification, 5000);
        }); 

Upvotes: 1

Related Questions